TypeError: 'str' object is not callable in python selenium code

TypeError: 'str' object is not callable in python selenium code

HTML:

我已经创建了一个 xpath

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

但每次我得到

TypeError: 'str' object is not callable

似乎没有得到 xpath。

可能是 xpath wrong.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> 

in ...

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

find_element_by_* 命令在最新的 Selenium Python 库中是 ,您必须使用 find_element() 代替。


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

You need to add the following import:

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

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

这不是真正的答案,而是对 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。那不是重点。报错是:

回溯(最后一次调用): 文件“/Volumes/G-DRIVE Thunderbolt 3/PythonStuff/Oct07.py”,第 9 行,位于 driver.find_element(By.XPATH('//h4[包含(text(),"这个周末")]')) 类型错误:'str' 对象不可调用

我觉得这像是一个错误