Selenium Webdriver - 无法找到元素
我有一个Selenium Webdriver JUnit Java测试,我无法在Drupal 8表(也有样式)中成功使用定位器。在Firefox中,我可以使用Firepath和Xpath检查器来确定并检查xpath确定。但是,当我运行JUnit 4测试时,无法找到它。有趣的是,当我第一次运行这个测试时,它运行正常,然而之后的很多尝试都不会运行。Selenium Webdriver - 无法找到元素
我已经在这个网站上成功地使用了xpath等许多其他的测试,但由于某种原因,我无法得到这个工作。
其实无法定位元素消息是为下一个命令;
driver.findElement(By.id("edit-application-status-1")).click();
但是我知道这是在以前的命令(如下)失败,因为它从来没有点击去这个页面。
失败代码;
driver.findElement(By.xpath(".//*[@id='block-ua-theme-content']/div/div/div[2]/table/tbody/tr[1]/td[2]/a")).click();
也试过;
driver.findElement(By.cssSelector("a[href='/application-19']")).click();
我试图改变到cssSelector
与显式等待如下
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[href='/application-19']")));
滚动到视图中,如下;
WebElement element1 = driver.findElement(By.cssSelector("a[href='/application-19']"));
jse.executeScript("arguments[0].scrollIntoView(true);", element1);
处理Web表格可能会很棘手,所以最好遍历WebTable的所有元素;然后检查,如果你的元素已经被发现,然后触发你的行动:
List<WebElement> tableElements = driver.findElements(By.xpath(".//*[@id='block-ua-theme-content']/div/div/div[2]/table/tbody/tr[1]/td"));
for(WebElement item : tableElements){
System.out.println("item text : " + item.getText());
if(item.getText().equals("Text of your <a> tag")){
WebElement newItem = item.findElement(By.tagName("a"));
newItem.click();
}
}
感谢。我试过这个,但它也没有工作?列表
什么是错误? – kushal
无法找到元素。但是这是下一个命令。它似乎在不点击该项目的情况下跳过此代码。 –
//Click on first item in list tr[1]
System.out.println("Before");
List<WebElement> tableElements = driver.findElements(By.xpath(".//[@id='block-ua-theme-content']/div/div/div[2]/table/tbody/tr[1]"));
for(WebElement item : tableElements){
if(item.getText().equals("Abhimanyujeet")){
WebElement newItem = driver.findElement(By.xpath("/a"));
System.out.println("tableElements" + tableElements);
System.out.println("item" + item);
newItem.click();
}
}
System.out.println("After");
driver.findElement(By.id("edit-application-status-1")).click();
控制台;
之前
tableElements[EyesRemoteWebElement:[[FirefoxDriver: firefox on XP (acecd5e9- e491-4afd-9f78-82351e50b9e4)] -> xpath: .//*[@id='block-ua-theme-content'] /div/div/div[2]/table/tbody/tr[1]]]
itemEyesRemoteWebElement:[[FirefoxDriver: firefox on XP (acecd5e9-e491-4afd- 9f78-82351e50b9e4)] -> xpath: .//*[@id='block-ua-theme-content']/div/div/div[2]/table/tbody/tr[1]]
后
跟踪;
org.openqa.selenium.NoSuchElementException: Unable to locate element: #edit\-application\-status\-1
因此错误是''edit-application-status-1''尝试使用'driver.findElement(By.cssSelector(“#edit-application-status-1”))。click();' – kushal
报告的错误是“编辑应用程序状态-1”;然而这不是真正的问题。该元素在下一页。问题在于这个元素; 。driver.findElement(By.cssSelector( “一个[HREF = '/应用-18']”))点击();永远不会被点击,所以它似乎跳过了这一点,并尝试点击“编辑 - 应用程序状态-1”这当然将失败,因为我们还没有在该页面上。 –
我认为这是一个普遍问题。看来Selenium认为它已经点击了一个项目,因此移动到下一行失败的代码行,因为之前的项目没有被点击,这意味着下一个元素当然不存在。 –
分享网站的URL或HTML代码 –
并请分享webdriver的初始化代码,看看你所使用的执行 –