微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

is_enabled 每次在 Python Selenium 循环中返回 true

如何解决is_enabled 每次在 Python Selenium 循环中返回 true

我有下面的代码搜索一个元素。如果未找到该元素,则单击下一页。我想要的是,如果直到最后一页才找到元素,它应该打印“找不到元素”。

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>

有人可以建议替代方法吗?

提前致谢!!!

解决方法

试试下面的代码一次。

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
,

我猜你的问题是缩进。看起来 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
,

我假设您遇到了时间问题。我认为避免它的最佳方法是在尝试单击或检查启用之前确保页面已更改。在 Selenium 术语中,当页面或页面的一部分通过导航到新页面或刷新页面的一部分等发生变化时,元素引用已经“过时”。我们可以检测到这种情况的方法是使用 { {1}} 并等待 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 )。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。