【发布时间】:2020-10-09 23:49:17
【问题描述】:
我正在尝试从网页中只有 divelements 的表格中获取值。没有tr td。下面是 HTML 的外观示例(不是真实示例)。
<div class="table" style="style="transform: translate3d(0px, 0px, 0px); opacity: 1;">
<div class="row heading">
<div class="row" style="opacity: 1;">
<div class="cell">
<div class="row1">value1</div>
<span class="Tooltip" style="style="position: relative;">...</span>
</div>
<div class="cell1">
<div class="classname"></div>
<div class="row1">value2</div>
</div>
<div class="cell1">
<div class="classname"></div>
<div class="row1">value3</div>
</div>
<div class="cell1">
<div class="classname"></div>
<div class="row1">value4</div>
</div>
<div class="cell1">
<div class="classname"></div>
<div class="row1">value5</div>
</div>
该表有 50 行,每行 5 列。我尝试了 2 种方法来获取表中的所有值。
-
使用 CSSSelector
for(int i=2;i<=38;i++) { WebDriverWait wait = new WebDriverWait(driver, 60); wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#root > div > div.Home > div.home-left > div.table > div:nth-child("+i+") > div:nth-child(1) > div"))); String valueone = driver.findElement(By.cssSelector("#root > div > div.Home > div.home-left > div.table > div:nth-child("+i+") > div:nth-child(1) > div")).getText(); clist.add(valueone); for(int j=2;j<=5;j++) { val = driver.findElement(By.cssSelector("#root > div > div.Home > div.home-left > div.table > div:nth-child("+i+") > div:nth-child("+j+") > div.total")).getText(); clist.add(val); } } -
使用 Xpath:
for(int i=2;i<=38;i++) { String c; WebDriverWait wait = new WebDriverWait(driver, 30); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"root\"]/div/div[3]/div[1]/div[5]/div["+i+"]/div[1]/div"))); valueone = driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[3]/div[1]/div[5]/div["+i+"]/div[1]/div")).getText(); clist.add(valueone); } for(int j=2;j<=38;j++) { c = driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[3]/div[1]/div[5]/div["+j+"]/div[2]/div[2]")).getText(); clist.add(c); } for(int k=2;k<=38;k++) { c = driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[3]/div[1]/div[5]/div["+k+"]/div[3]/div")).getText(); clist.add(c); } for(int l=2;l<=38;l++) { c = driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[3]/div[1]/div[5]/div["+l+"]/div[4]/div[2]")).getText(); clist.add(c); }
问题:当我尝试使用 css 选择器进行迭代时,我得到了这个异常(找不到第 12 行)-
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.cssSelector: #root > div > div.Home > div.home-left > div.table > div:nth-child(12) > div:nth-child(1) > div (tried for 30 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
at App.GetData.main(GetData.java:46)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#root > div > div.Home > div.home-left > div.table > div:nth-child(12) > div:nth-child(1) > div"}
当我尝试使用 X Path 进行迭代时,我得到了这个异常(找不到第 13 行)-
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //*[@id="root"]/div/div[3]/div[1]/div[5]/div[13]/div[1]/div (tried for 30 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
at App.GetData.main(GetData.java:72)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="root"]/div/div[3]/div[1]/div[5]/div[13]/div[1]/div"}
我试过ImplicitWait、ExplicitWait 甚至Thread.sleep(我知道不推荐)。似乎没有任何工作。 具有讽刺意味的是,如果我尝试 20 次,Selenium 会在 1 次尝试中识别出所有没有问题的元素。
-
为什么 css 选择器很难找到一个元素,而 xpath 是找到该元素但很难找到另一个元素?
-
我在 Selenium 中经常遇到这个问题,当它能够在一次运行中毫无问题地找到相同的元素并且在其他运行中难以找到它时?是否有针对此问题的永久解决方案/方法?
【问题讨论】:
标签: selenium selenium-webdriver xpath css-selectors nosuchelementexception