我不確定python的語法,所以我會用Java代替。我認爲有兩種可能的情況,首先你的驅動程序在頁面加載時查看DOM結構。另一個是網絡驅動程序沒有指向DOM的根源。我通常有等待方法等待頁面加載完成,並在查找某些元素之前切換到默認內容。
首先,我通過構造函數傳遞WebDriver,Element id和Element名稱。然後我有waitUntilReady()
執行邏輯來檢查特定超時內元素的存在。
public void waitUntilReady() {
WebDriverWait waiter = new WebDriverWait(getWebDriver(), 30);
waiter.withMessage("\"" + getElementName() + "\" is not ready in 30 seconds")
.until(new Predicate<WebDriver>() {
@Override
public boolean apply(WebDriver input) {
/* isReady() returns true if all elements
* that you need to test are available.
* Note that if you have javascript, you
* should have some hidden field to tell
* you that the whole page has loaded. */
return isReady();
}
});
}
以及開關默認內容之前找到元素
WebElement getWebElement() {
WebElement webElement;
try {
getWebDriver().switchTo().defaultContent();
webElement = getWebDriver().findElement(By.id(elementId));
} catch (NoSuchElementException ex) {
getLogger().log(Level.SEVERE, "Element id '" + elementId + "' does not exist");
throw ex;
}
return webElement;
}
如果它不工作,你可以有<body id="container">
,然後用driver.findElementById( 「容器」)。findElementById( 「你的目標元素」)
我對Java並不熟悉。我用
得到你正在說的,然後找到元素byid方法。我需要python語法來查找元素中的元素。 – MrGlass