繁体   English   中英

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

[英]is_enabled returns true each time in the loop for Python Selenium

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

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块的缩进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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM