Selenium webdriver 元素识别在机器生成的网页上失败

Selenium webdriver element identification fails on machine-generated webpage

我在识别由机器生成的网页上的元素时遇到了问题。我将 Selenium Webdriver 用于 Java 和 Google Chrome 作为浏览器。

因此,有些 ID 和值看起来很不寻常。到目前为止,我试图通过 id 和 xpath 来识别元素。这是我的问题的一些代码。

我要识别的select盒子:

<select id="singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person" class="form-control" aria-expanded="false" aria-labelledby="lbl_192_982637" aria-describedby="singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person_Sth"><option aria-selected="false" disabled="" style="display:none" aria-hidden="true"></option><option id="singleselect-option-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person[0]" value="OK" aria-selected="false">Ja, die Gehaltsnachweise liegen vor</option><option id="singleselect-option-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person[1]" value="NOK" aria-selected="false">Ja, es bestehen aber Formmängel</option><option id="singleselect-option-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person[2]" value="NO" aria-selected="false">Nein, die Gehaltsnachweise liegen nicht vor</option><option id="singleselect-option-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person[3]" value="WAIVER" aria-selected="false">Unterlagsverzicht</option></select>

完整的 XPath 是:

/html/body/div[1]/div[2]/div[2]/div[1]/div[2]/div/div[2]/div/div/div/div[2]/div[1]/div[2]/div/div[2]/div/div[2]/div/div[1]/div/div/select

相对 xPath 是:

//*[@id="singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person"]

通过 id ("singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person") 识别失败也像 xpath,即使在 Google Chrome 中输入的 xpath 是有效的:

不幸的是,我无法 post 整个 html 页面,但也许有人在这里有线索。它是一个大页面,也许滚动是问题,因为并非所有项目在 window?

中都是可见的艺术一次

从第一个评论开始的最后一次尝试(检查先决条件 1/1)

翻译成Java:

WebDriverWait wait = new WebDriverWait(driver, 30);
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select[id^='singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:'][aria-describedby^='singleselect-Vertical_Layout']")));
        WebElement select = driver.findElement(By.cssSelector("select[id^='singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:'][aria-describedby^='singleselect-Vertical_Layout']"));

...失败:

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.cssSelector: select[id^='singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:'][aria-describedby^='singleselect-Vertical_Layout'] (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 com.commerzbank.test.tool.service.e2e.ChromeTest.main(ChromeTest.java:74)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"select[id^='singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:'][aria-describedby^='singleselect-Vertical_Layout']"}
  (Session info: chrome=97.0.4692.71)

首先你需要检查下面是否CSS:

select[id^='singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:'][aria-describedby^='singleselect-Vertical_Layout']

在 HTMLDOM 中是否唯一。

检查步骤:

Press F12 in Chrome -> 转到 element 部分 -> 执行 CTRL + F -> 然后粘贴 css 并查看是否需要 element正在 突出显示 1/1 匹配节点。

如果它是唯一的,我建议您尝试使用显式等待。

代码:

wait = WebDriverWait(driver, 30)
select = Select(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "select[id^='singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:'][aria-describedby^='singleselect-Vertical_Layout']"))))
select.select_by_value('NOK')

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

长时间的研究,简短的回答。问题是在我的大网页上有一些嵌套很深的 iframe,在我的特殊情况下有两个。我用下面的代码片段解决了它:

 wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//iframe[contains(@title, 'Manual')]")));
        WebElement parentFrame = driver.findElement(By.xpath("//iframe[contains(@title, 'Manual')]"));
        driver.switchTo().frame(parentFrame);
        wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath("//iframe[contains(@title, 'Test')]"))));
        WebElement childFrame = driver.findElement(By.xpath("//iframe[contains(@title, 'Test')]"));
        driver.switchTo().frame(childFrame);

切换到子 iframe 后,我可以找到我的元素。干杯。