Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

python - TypeError: 'str' object is not callable when trying to click "datepicker"

The relevant HTML

<div id="datepickerbox" class="ym-gbox-left">
 <div class="datepick_label">
  <div id="datepicker" class="hasDatepicker">
   <div class="ui-datepicker-inline ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all" style="display: block;">

My code

self.driver.find_element(By.XPATH('//*[@id="datepicker"]')).click()
dateWidget = self.driver.find_element(By.ID("ui-datepicker-div"))
rows = dateWidget.find_element_by_tag_name("tr")
columns = dateWidget.find_element_by_tag_name("td")
for cell in columns: self.driver.findElement(By.LINK_TEXT("20")).click()

The error I'm getting

self.driver.find_element(By.XPATH('//*[@id="datepicker"]')).click()
TypeError: 'str' object is not callable

How can I make this work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the “Location elements” section of the manual, By.XPATH seems to be a string, and the correct usage would be this:

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

That also makes sense from the error message, since you are calling By.XPATH. And the current source also confirms this.

Unfortunately, the Selenium API is not really consistent across different platforms. The syntax you are using, calling By.xpath() is used by Java, and in C#, you also have to call By.Xpath(). So it’s just naturally that you get confused when you see other examples where it’s being called and in Python, you suddenly need to pass it. So you shouldn’t blindly trust examples you find on the internet (like this one) but always check for which language they are. And check the documentation and/or the source in doubt.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...