51Testing软件测试论坛
标题:
webdriver无法定位frame中按钮
[打印本页]
作者:
Congdinal1han
时间:
2015-5-31 21:07
标题:
webdriver无法定位frame中按钮
用webdriver录脚本 ,可以定位到frame中的元素,但无法定位到该frame页面的按钮,求指导,弄了好几天,没弄出来。下面把代码贴出来
package com.selenium.test;
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.Set;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import com.gargoylesoftware.htmlunit.javascript.host.Document;
public class testamar {
@Test
public void test() throws InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.get("http://app.amarsoft.com/ALS745");
Thread.sleep(5000);
WebElement username =
driver.findElement(By.className("input_class"));
username.sendKeys("test11");
Thread.sleep(5000);
//WebElement btn =
driver.findElement(By.className("button_submit"));
WebElement btn = driver.findElement(By.id("btn_submit"));
btn.click();
Thread.sleep(10000);
WebElement link =
driver.findElement(By.xpath("//ul[@id='ASMenuBar_ALS7']/li[2]/a/span[2]"));
link.click();
WebElement link2 =
driver.findElement(By.xpath("//ul[@id='ASMenuBar_ALS7']/li[2]/ul/li/a/span[3]"));
link2.click();
Thread.sleep(5000);
driver.switchTo().frame("SecondFrame_");
//点击新增按钮
String js = "setTimeout(function(){document.getElementsByClassName('btn_text')[0].click()},100)";
((JavascriptExecutor)driver).executeScript(js);
Thread.sleep(2000);
Set<String> handlers = driver.getWindowHandles();
for(String winHandler:handlers){
driver.switchTo().window(winHandler);
}
WebElement close = driver.findElement(By.xpath("//span/a[2]"));
// close.click();
//输入新增页面幸免元素值
driver.switchTo().frame("ObjectList");
//填写数据
Select select = new
Select(driver.findElement(By.id("ORGNATURE")));
select.selectByVisibleText("事业单位");
Select select2 = new
Select(driver.findElement(By.id("CERTTYPE")));
select2.selectByVisibleText("营业执照");
WebElement creditid = driver.findElement(By.id("CERTID"));
creditid.sendKeys("11223344");
WebElement recreditid = driver.findElement(By.id("RECERTID"));
recreditid.sendKeys("11223344");
Select select3 = new
Select(driver.findElement(By.id("CUSTOMERTYPE")));
select3.selectByVisibleText("大型企业");
WebElement indexpiry = driver.findElement(By.id("IDEXPIRY"));
indexpiry.sendKeys("2016/09/21");
WebElement cusname = driver.findElement(By.id("CUSTOMERNAME"));
cusname.sendKeys("nana的客户");
// 确认按钮,无法定位,报错,unable to locate element;
WebElement sure =driver.findElement(By.cssSelector("span[clss='btn_text']"));
sure.click();
String title = driver.getTitle();
String currentUrl = driver.getCurrentUrl();
System.out.println(title+"\n"+currentUrl);
//driver.quit();
}
}
作者:
jingzizx
时间:
2015-6-1 13:44
看你代码,'btn_text'是个class;
改成这样试试WebElement sure =driver.findElement(By.cssSelector("botton.btn_text"));
作者:
congdina
时间:
2015-6-2 14:35
你好,试过了,还是报一样的错,弹出的是模态窗口,窗口里的字段都可以获取到,就是确认铵钮获取不到,能帮我调试下么,上面代码可以直接运行的,非常感谢
作者:
congdina
时间:
2015-6-4 09:09
已经解决了,之前一直用XPATH,估计路径写的不对,昨天研究了一下cxxSelector,还是很好用的
List<WebElement> sure = driver.findElements(By.cssSelector("span.btn_text"));
for (WebElement s : sure){
if(s.getText().equals("确认"))
{
System.out.println("找到确认铵钮");
s.click();
Thread.sleep(5000);
}
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2