【问题标题】:Error: "'str' object is not callable" on any use of Xpath [duplicate]错误:在任何使用 Xpath 时“'str' 对象不可调用”[重复]
【发布时间】:2021-11-18 14:48:44
【问题描述】:

我正在尝试在 Selenium 中编写一些测试,并且由于许多元素没有 ID 或名称,因此我尝试使用 Xpath。但是,无论如何,每当我使用 Xpath 时,我都会遇到相同的错误:

TypeError: 'str' 对象不可调用

以下是我测试过的最简单的示例,它会导致相同的错误。

这是 Python 中的代码:

from selenium.webdriver.common.by import By
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('ignore-certificate-errors')

driver = webdriver.Chrome(chrome_options=options)
driver.get('cannot_share_this_link/index.php')

driver.find_element(By.XPATH("//*[@id='sign']"))

测试日期:

<html>
    <head>
        <title>
            Comtest
        </title>
        <script src="comajax.js">
        </script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js">
        </script>
    </head>

    <body>
         <div>
         <br>
         <input type="button" id="sign" value="Sign up" onclick="window.open('signup.php','popUpWindow','height=500,width=800,left=700,top=300,resizable=yes,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no, status=yes');">
         <br>
         <input type="button" id="log" value="Log in" onclick="window.open('login.php','popUpWindow','height=500,width=800,left=700,top=300,resizable=yes,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no, status=yes');">
         </div>
    </body>
</html>

编辑:我想指出,这个问题不是重复的,因为链接的线程显然是在寻找一个字符串作为输出,而我正在寻找一个元素。此外,两年前在那里展示的方法现在会抛出弃用警告,如果它们甚至可以工作(因为我根本无法让它工作,我无法判断)。 我已经阅读了链接的主题,在寻找解决方案的过程中我偶然发现了它大约 30 次。可能没有关于这个问题的帖子我没有读过,否则我不会发帖。

【问题讨论】:

    标签: python selenium xpath


    【解决方案1】:

    代替

    driver.find_element(By.XPATH("//*[@id='sign']"))
    

    使用这个:

    driver.find_element(By.XPATH, "//*[@id='sign']")
    

    内部方法签名:

    def find_element(self, by=By.ID, value=None):
    

    预计使用 By then 用逗号分隔,然后是 value。

    value 可以是您的 xpath,以防 By 是您提供的 xpath。

    我建议诱导Explicit wait (WebDriverWait)

    代码试用:

    sign_in_ele = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='sign']")))
    sign_in_ele.click()
    

    进口:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

    【讨论】:

    • 我已经尝试过了,当我使用它时,它表明 "//*[@id='sign']" 中的 "sign" 是一个合成错误。刚刚又试了一次。
    • 你试过了吗sign_in_ele = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='sign']")))
    • 是的,在实际程序和我测试过的所有网站上,我都有适当的等待。这里没有,因为站点会立即加载。我总是用基于 id 的等待来测试它。
    【解决方案2】:

    请尝试更换:

    driver.find_element(By.XPATH("//*[@id='sign']"))
    

    与:

    driver.find_element_by_xpath('//*[@id="sign"]'))
    

    【讨论】:

    • 当我尝试使用它时,我得到了弃用错误:/home/install/PycharmProjects/Comtest/main.py:10: DeprecationWarning: find_element_by_* 命令已弃用。请使用 find_element() 代替 driver.find_element_by_xpath('//*[@id="sign"]')
    • 这是一个警告,但它会运行吗?
    • 我尝试使用 .click() 但它没有发生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    相关资源
    最近更新 更多