在 selenium2 中定位元素时出错
我正在使用 selenium2.0 和 testNG。当使用 XPATH 或 CSS 元素来定位其时,会显示错误“无法定位该元素”。 我用Java编程为 下面:
public class mytest {
public static WebDriver driver;
public Alert alert;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
driver=new FirefoxDriver();
driver.get("http://localhost:4503/xyz.html");
}
public static void clickButton(WebDriver driver, String identifyBy, String locator){
if (identifyBy.equalsIgnoreCase("xpath")){
driver.findElement(By.xpath(locator)).click();
}else if (identifyBy.equalsIgnoreCase("id")){
driver.findElement(By.id(locator)).click();
}else if (identifyBy.equalsIgnoreCase("name")){
driver.findElement(By.name(locator)).click();
}
}
public static void typeinEditbox(WebDriver driver, String identifyBy, String locator, String valuetoType){
if (identifyBy.equalsIgnoreCase("xpath")){
driver.findElement(By.xpath(locator)).sendKeys(valuetoType);
}else if (identifyBy.equalsIgnoreCase("id")){
driver.findElement(By.id(locator)).sendKeys(valuetoType);
}else if (identifyBy.equalsIgnoreCase("name")){
driver.findElement(By.name(locator)).sendKeys(valuetoType);
}
}
public static void openApplication(WebDriver driver, String url) {
driver.get(url);
}
@Test
public void testAcrolinxApplication() throws InterruptedException {
openApplication(driver,"http://xyz.com");
typeinEditbox(driver,"name","p_user","xxx");
typeinEditbox(driver,"name","p_pas","yyy");
clickButton(driver,"id","input-submit");
/*Up to this its working fine …..
At below line this throws error could not locate the xpath element "//a[@id='cq-gen100']/em/span/span" BUT THIS IS WOKING FINE IN Selenium1.0 api that is with
selenium.click("//a[@id='cq-gen100']/em/span/span"); */
driver.findElement(By.xpath("//a[@id='cq-gen100']/em/span/span")).click();
}
}
请帮我解决这个问题。 提前致谢…
I am using selenium2.0 with testNG. While on using XPATH or CSS for element to locate its shows error “unable to locate the element” .
I have programmed in Java as
below:
public class mytest {
public static WebDriver driver;
public Alert alert;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
driver=new FirefoxDriver();
driver.get("http://localhost:4503/xyz.html");
}
public static void clickButton(WebDriver driver, String identifyBy, String locator){
if (identifyBy.equalsIgnoreCase("xpath")){
driver.findElement(By.xpath(locator)).click();
}else if (identifyBy.equalsIgnoreCase("id")){
driver.findElement(By.id(locator)).click();
}else if (identifyBy.equalsIgnoreCase("name")){
driver.findElement(By.name(locator)).click();
}
}
public static void typeinEditbox(WebDriver driver, String identifyBy, String locator, String valuetoType){
if (identifyBy.equalsIgnoreCase("xpath")){
driver.findElement(By.xpath(locator)).sendKeys(valuetoType);
}else if (identifyBy.equalsIgnoreCase("id")){
driver.findElement(By.id(locator)).sendKeys(valuetoType);
}else if (identifyBy.equalsIgnoreCase("name")){
driver.findElement(By.name(locator)).sendKeys(valuetoType);
}
}
public static void openApplication(WebDriver driver, String url) {
driver.get(url);
}
@Test
public void testAcrolinxApplication() throws InterruptedException {
openApplication(driver,"http://xyz.com");
typeinEditbox(driver,"name","p_user","xxx");
typeinEditbox(driver,"name","p_pas","yyy");
clickButton(driver,"id","input-submit");
/*Up to this its working fine …..
At below line this throws error could not locate the xpath element "//a[@id='cq-gen100']/em/span/span" BUT THIS IS WOKING FINE IN Selenium1.0 api that is with
selenium.click("//a[@id='cq-gen100']/em/span/span"); */
driver.findElement(By.xpath("//a[@id='cq-gen100']/em/span/span")).click();
}
}
Please help me on this.
Thanks in advance…
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保 XPath/CSS 查询正确
下载类似 Firefinder 或其他任何可以直接在页面上测试 XPath 查询的东西。一旦你确定你的查询是正确的,那么你就可以缩小 Selenium 中的问题范围。
缩小 Selenium 中的问题范围
例如,如果您的查询
"//a[@id='cq-gen100']/em/span/span"
不起作用,尝试查询的基础并查看 Selenium 是否找到它(现在不用担心点击)。从那里递增,直到错误再次出现。例子:
Make Sure XPath/CSS Query is Correct
Download something like Firefinder or anything else where you can test the XPath query directly on the page. Once you know for sure that your query is correct, then you can narrow down the problem in Selenium.
Narrow Down the Problem in Selenium
For example, if your query
"//a[@id='cq-gen100']/em/span/span"
is not working, try the base of the query and see if Selenium finds it (don't worry about click for now). Increment from there until the error appears again.Example:
我用过
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
等待一段时间,Noew 就可以工作了。
I have used
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
to wait for some time, Noew its working.