首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >is_enabled每次在Python循环中返回true

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

Stack Overflow用户
提问于 2021-07-30 12:36:08
回答 3查看 389关注 0票数 0

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

代码语言:javascript
代码运行次数:0
运行
复制
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代码如下:

用于禁用按钮的

代码语言:javascript
代码运行次数:0
运行
复制
<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>

用于普通按钮

代码语言:javascript
代码运行次数:0
运行
复制
<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>

有人能建议另一种方法吗?

提前谢谢!

EN

回答 3

Stack Overflow用户

发布于 2021-07-30 13: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})]"

所以,请试试这个:

代码语言:javascript
代码运行次数:0
运行
复制
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
票数 1
EN

Stack Overflow用户

发布于 2021-07-30 13:05:17

尝试下面的代码一次。

代码语言:javascript
代码运行次数:0
运行
复制
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
票数 0
EN

Stack Overflow用户

发布于 2021-07-30 21:27:00

我假设你遇到了时间问题。我认为避免这种情况的最好方法是在尝试单击或检查启用之前确保页面已经更改。在Selenium术语中,当页面或页面的一部分通过导航到新页面或刷新页面的一部分等方式发生更改时,元素引用已经“陈旧”了。我们可以通过使用WebDriverWait和等待staleness_of来检测这一点。

我还将定位器分解到代码块的顶部,因为这样可以使代码更易于阅读,而且如果发生变化,定位器也更容易管理。

请参阅下面我对您代码的更新。

代码语言:javascript
代码运行次数:0
运行
复制
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)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68591073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档