下面有搜索元素的代码。如果找不到该元素,请单击下一页。我想要的是,如果元素直到最后一页才被找到,它应该打印“元素未找到”。
elpath=f"//span[contains(text(),[value})]"
while True:
time sleep(2)
try:
driver.find element_by_xpath(elpath).click()
break
except Exception:
if driver.find element_by_xpath("Xpath to click Next Page").is_enabled():
driver.find_element_by_xpath("Xpath to click Next Page").click()
else:
print("Element Not Found")
break
但是,当我选中driver.find element_by_xpath("Xpath to click Next Page").is_enabled()
时,即使禁用next按钮(即列表的最后一页),也会返回True。
下一个按钮的HTML代码如下:
用于禁用按钮的
<button mat-icon-button="" type="button" class="mat-focus-indicator mat-tooltip-trigger mat-paginator-navigation-next mat-icon-button mat-button-base_mat-animation-noopable mat-button-disabled" aria-label="Next page" disabled="true">
<span class="mat-button-wrapper">
<svg viewBox="0 0 24 24" focusable="false" class="mat-paginator-icon"> <path d="M08 6G8.59 8.32 13.23 121-821 4L10 142-6b">
</Path>
</svg>
</span>
<span matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round"> </span><span class="mat-button-focus-overlay">
</span>
</button>
用于普通按钮
<button mat-icon-button="" type="button" class="mat-focus-indicator mat-tooltip-trigger mat-paginator-navigation-next mat-icon-button mat-button-base_mat-animation-noopable" aria-label="Next page">
<span class="mat-button-wrapper">
<svg viewBox="0 0 24 24" focusable="false" class="mat-paginator-icon"> <path d="M08 6G8.59 8.32 13.23 121-821 4L10 142-6b">
</Path>
</svg>
</span>
<span matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round">
</span>
<span class="mat-button-focus-overlay">
</span>
</button>
有人能建议另一种方法吗?
提前谢谢!
发布于 2021-07-30 05:42:41
我猜你的问题是压痕。看起来time sleep(2)
不像内部try-except
块具有相同的缩进。
这导致单击next page
按钮后,下一页仍未加载,因此Selenium实际上是上一页next page
元素。
另外,您有一个错误,您缺少了_
--应该是find_element_by_xpath
。
同时,在elpath=f"//span[contains(text(),[value})]"
中输入错误,而它应该是elpath=f"//span[contains(text(),{value})]"
所以,请试试这个:
elpath=f"//span[contains(text(),{value})]"
while True:
time sleep(2)
try:
driver.find_element_by_xpath(elpath).click()
break
except Exception:
if driver.find_element_by_xpath("Xpath to click Next Page").is_enabled():
driver.find_element_by_xpath("Xpath to click Next Page").click()
else:
print("Element Not Found")
break
发布于 2021-07-30 05:05:17
尝试下面的代码一次。
try:
driver.find element_by_xpath(elpath).click()
break
except Exception:
if driver.find element_by_xpath("Xpath to click Next Page").get_attribute('disabled') == None:
driver.find_element_by_xpath("Xpath").click()
elif driver.find element_by_xpath("Xpath").get_attribute('disabled') == "true":
print("Element Not Found")
break
发布于 2021-07-30 13:27:00
我假设你遇到了时间问题。我认为避免这种情况的最好方法是在尝试单击或检查启用之前确保页面已经更改。在Selenium术语中,当页面或页面的一部分通过导航到新页面或刷新页面的一部分等方式发生更改时,元素引用已经“陈旧”了。我们可以通过使用WebDriverWait
和等待staleness_of
来检测这一点。
我还将定位器分解到代码块的顶部,因为这样可以使代码更易于阅读,而且如果发生变化,定位器也更容易管理。
请参阅下面我对您代码的更新。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
ele_locator=(By.XPATH, "//span[contains(text(),[value])]")
next_page_locator = (By.XPATH, "next page XPath")
wait = new WebDriverWait(driver, 10)
while True:
try:
driver.find_element(ele_locator).click()
break
except Exception:
next_page = driver.find_element(next_page_locator)
if next_page.is_enabled():
next_page.click()
wait.until(EC.staleness_of(ele))
else:
print("Element Not Found")
break
注意:如果您使用的是隐式等待,则需要关闭它们。在文档中明确警告混合隐式和显式等待(WebDriverWait
)。
https://stackoverflow.com/questions/68591073
复制相似问题