【问题标题】:TypeError: 'str' object is not callable in python selenium codeTypeError:'str'对象在python selenium代码中不可调用
【发布时间】:2022-01-08 07:30:38
【问题描述】:

HTML:

我已经创建了一个 xpath

driver.find_element(By.XPATH('//h4[contains(text(),"This weekend")]'))

但每次我都得到

TypeError: 'str' object is not callable

它似乎没有得到 xpath。

可能是 xpath 似乎是错误的。cananyone 在这里指导

HTML:

<a class="css-t8q75u e19c3kf61" xpath="1"><div class="css-ekw2cu eh8fd9012"><div class="css-3ov7b7 eh8fd9011"><div class="css-95s8c1 eh8fd9010"><header class="css-1p61d40 eh8fd909">** are not unique 

<a class="css-t8q75u e19c3kf61" xpath="1"><div class="css-ekw2cu eh8fd9012"><div class="css-3ov7b7 eh8fd9011"><div class="css-95s8c1 eh8fd9010"><header class="css-1p61d40 eh8fd909"><span class="css-m74hjb e19c3kf60"><span class="icon css-1ih0xvs e1ouvt3m1"><svg aria-label="CalendarWeekend" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="1.414" xmlns="http://www.w3.org/2000/svg" role="presentation" focusable="false" viewBox="0 0 32 32" preserveAspectRatio="xMidYMid meet" class="css-1lisf4l e1ouvt3m0"><path d="M28 14v9a5.006 5.006 0 01-4.783 4.995L23 28H9a5.006 5.006 0 01-4.995-4.783L4 23v-9h24zm-4 3h-1a1 1 0 00-1 1v1a1 1 0 001 1h1a1 1 0 001-1v-1a1 1 0 00-1-1zm-5 0h-1a1 1 0 00-1 1v1a1 1 0 001 1h1a1 1 0 001-1v-1a1 1 0 00-1-1zm3-13a1 1 0 01.993.883L23 5v1a5.006 5.006 0 014.995 4.783L28 11v1H4v-1a5.006 5.006 0 014.783-4.995L9 6V5a1 1 0 011.993-.117L11 5v1h10V5a1 1 0 011-1z"></path></svg></span></span></header><h4 class="css-11rlpdz eh8fd905">This weekend</h4><div class="css-1bu4bq eh8fd908"><h5 class="css-j3w7e9 eh8fd904">Local events taking place on Friday, Saturday and Sunday</h5></div></div></div></div></a> 

【问题讨论】:

  • 什么是texttext() 看起来很可疑。看起来你正在尝试调用一个字符串。
  • 这个周末

    ..这是h4类中的文字
  • 我刚刚尝试了与此非常相似的方法并遇到了同样的问题。使用 Python 3.9.9 的最新版 selenium (4.1.0) 中可能存在错误?
  • @DarkKnight inside find_element() 你需要发送一个By定位器。
  • @DebanjanB By.XPATH

标签: python selenium-webdriver xpath automation selenium-chromedriver


【解决方案1】:

按照中的DeprecationWarning ...

DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead

find_element_by_* 命令在最新的 Selenium Python 库中是 deprecated,您必须改用 find_element()


要将字符序列发送到文本字段,您可以使用以下任一Locator Strategies

您需要添加以下导入:

from selenium.webdriver.common.by import By
  • 使用xpath

    driver.find_element(By.XPATH, '//h4[contains(text(),"This weekend")]')
    

【讨论】:

  • OP 正在使用 find_element。这也是我尝试并观察到同样的错误
  • @DarkKnight 仔细检查()的语法和位置
  • 我已经检查过了。看我的回答。我认为某处存在错误
  • @AbdulSaboor 很高兴能为您提供帮助! Upvote如果这个/任何答案对您/对未来的读者有帮助,那么答案。
【解决方案2】:

这并不是真正的答案,而是对 OP 错误的确认。需要明确的是,这是 selenium 4.1.0、Python 3.9.9、macOS 12.0.1

from selenium import webdriver
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-extensions')
with webdriver.Chrome(options=options) as driver:
    driver.implicitly_wait(5)
    driver.get('https://google.com')
    driver.find_element(By.XPATH('//h4[contains(text(),"This weekend")]'))

显然,我不希望找到带有“本周末”作为文本的 h4。那不是重点。报错是:

Traceback(最近一次调用最后一次): 文件“/Volumes/G-DRIVE Thunderbolt 3/PythonStuff/Oct07.py”,第 9 行,在 driver.find_element(By.XPATH('//h4[contains(text(),"本周末")]')) TypeError: 'str' 对象不可调用

这对我来说似乎是一个错误

【讨论】:

  • 嘿,我用过 (By.XPATH, '//h4[contains(text(),"This week")]' 这个语法,它确实有效
  • 你用的是什么版本?
猜你喜欢
  • 2023-01-15
  • 2020-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
  • 2012-06-04
  • 1970-01-01
相关资源
最近更新 更多