简体   繁体   中英

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

HTML:

在此处输入图像描述

I have created a xpath

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

But every time I'm getting

TypeError: 'str' object is not callable

It seems it is not getting the xpath.

May be the xpath seems wrong.cananyone guide here

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> 

Following the DeprecationWarning in ...

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

find_element_by_* commands are deprecated in the latest Selenium Python libraries and you have to use find_element() instead.


To send a character sequence to the text field you can use either of the following Locator Strategies :

You need to add the following import:

 from selenium.webdriver.common.by import By
  • Using xpath :

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

This isn't really an answer but confirmation of the OP's error. To be clear, this is 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")]'))

Obviously, I don't expect to find an h4 with "This weekend" as text. That's not the point. The error reported is:

Traceback (most recent call last): File "/Volumes/G-DRIVE Thunderbolt 3/PythonStuff/Oct07.py", line 9, in driver.find_element(By.XPATH('//h4[contains(text(),"This weekend")]')) TypeError: 'str' object is not callable

This looks like a bug to me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM