我有下面的代码用于搜索元素。如果未找到该元素,则单击下一页。我想要的是,如果直到最后一页才找到该元素,它应该打印“未找到元素”。
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()
时,即使禁用下一个按钮(即列表的最后一页),也会返回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>
有人可以建议替代方法吗?
提前致谢!
我猜你的问题是缩进。看起来
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
尝试以下代码一次。
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
我假设您遇到了时间问题。我认为避免这种情况的最佳方法是在尝试单击或检查启用之前确保页面已更改。用 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
)。
if driver.find_element(By.XPATH,"//a[@arialabel='Next']").get_dom_attribute('aria-disabled')=='false': 下一个.click() 别的: 打破