python - 即使存在元素,WebdriverWait 也会失败

标签 python html selenium xpath selenium-webdriver

这是我的代码:

def CheckQueue(driver):
    qdone = False
    qID_xpath_start = "/html/body/div[5]/form/table/tbody[1]/tr["
    qID_xpath_end = "]/td[2]/a"
    qIDindex = 1
    while qdone == False:
        print "enter loop"
        print driver.find_element_by_xpath(qID_xpath_start+str(qIDindex)+qID_xpath_end).text #This prints
        try:
            element = WebDriverWait(driver, 6000).until(ec.presence_of_element_located(By.XPATH((qID_xpath_start+str(qIDindex)+qID_xpath_end)))) #This fails
            print "found"
        except:
            qdone= True
            print "could not be found"

        print driver.find_element_by_xpath(qID_xpath_start+str(qIDindex)+qID_xpath_end).text
        if qdone == False:
            print driver.find_element_by_xpath(qID_xpath_start+str(qIDindex)+qID_xpath_end).text
            print "testing"

        qIDindex +=1
        print "loop"
    return driver

我得到这个返回(14453 是我正在寻找的 xpath 的链接文本)

enter loop
14453
could not be found
14453
loop

看起来我的代码能够找到链接,但是当要求检查链接是否存在时,它失败了,并激活了 except 语句。如果已经找到并打印出来,为什么会失败?

此外,它几乎立即失败,即使我已经分配了很多时间来查看它。

知道我哪里出错了吗?

我试过了

element = WebDriverWait(driver, 6000).until(ec.presence_of_element_located(By.XPATH((qID_xpath_start+str(qIDindex)+qID_xpath_end))))

element = WebDriverWait(driver, 6000).until(EC.presence_of_element_located(By.XPATH((qID_xpath_start+str(qIDindex)+qID_xpath_end))))

element = WebDriverWait(driver, 6000).until(ec.presence_of_element_located(By.XPATH, qID_xpath_start+str(qIDindex)+qID_xpath_end))

element = WebDriverWait(driver, 6000).until(ec.presence_of_element_located(By.xpath, qID_xpath_start+str(qIDindex)+qID_xpath_end))

我正在使用 Python 2.7、Selenium 2.43、Firefox 23.0.3

顺便说一下,为了测试是否可以在某些点找到该 xpath 元素,我加入了几个可能感觉不合适的打印语句。

编辑:当我删除 try 语句时,出现此错误。

element = WebDriverWait(driver, 6000).until(EC.presence_of_element_located(By.XPATH((qID_xpath_start+str(qIDindex)+qID_xpath_end))))

NameError: 全局名称 'By' 未定义

我的代码中有以下导入语句:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains

我想我确实需要添加一个导入语句,但我似乎找不到如何导入该语句。

我试过:

from selenium.webdriver.common.by import By

当我运行它时,我克服了那个错误但收到了这个错误:

element = WebDriverWait(driver, 6000).until(EC.presence_of_element_located(By.XPATH((qID_xpath_start+str(qIDindex)+qID_xpath_end)))) TypeError: 'str' object is not callable

然后我更新了 WebDriverWait 行以反射(reflect) alecxe 的建议。

element = WebDriverWait(driver, 6000).until(EC.presence_of_element_located((By.XPATH, qID_xpath_start+str(qIDindex)+qID_xpath_end)))

现在它似乎正在工作。

最佳答案

WebDriverWait expression syntax不正确,应该是:

WebDriverWait(driver, 60).until(ec.presence_of_element_located((By.XPATH, qID_xpath_start+str(qIDindex)+qID_xpath_end)))

注意传入 presence_of_element_located() 方法的元组。

请注意,60 在这里是 60


此外,为了成功调试和理解正在发生的事情,让它失败通常会有所帮助 - 删除 try/except 并查看引发了什么样的错误。

关于python - 即使存在元素,WebdriverWait 也会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26267565/

相关文章:

javascript - 当游戏受到保护时,如何访问游戏的代码(即使是混淆的)?

python - Selenium 取元素截图 - Google Chrome

python 类型错误 : get_client_list() missing 1 required positional argument: 'limit'

python - 单个应用程序的 Django URL 路由

css - 根据父 Div 调整 Textarea 的大小

html - 如何使用 CSS 将 div 定位在页面中间同时支持动态宽度?

python-3.x - Selenium 隐式和显式等待不起作用/无效

java - 如何使用属性 'display:none' 获取 XPath 的 getText()

python - 将 django 项目编译为桌面应用程序

python - 根据目录名称 : What are the consequences? 使用 importlib 加载同名类