TypeError:尝试单击“日期选择器"时,无法调用"str"对象 [英] TypeError: 'str' object is not callable when trying to click "datepicker"

查看:101
本文介绍了TypeError:尝试单击“日期选择器"时,无法调用"str"对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关的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;">

我的代码

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()

我遇到的错误

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

我该如何进行这项工作?

How can I make this work?

推荐答案

根据By.XPATH似乎是字符串,正确的用法是:

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"]')

从错误消息中也可以理解,因为您正在调用By.XPATH.并且当前来源也可以确认这个.

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

不幸的是,Selenium API在不同平台之间并不是真正一致的. Java使用的是调用By.xpath()的语法,在C#中,还必须调用By.Xpath().因此,很自然地,当您看到其他示例被调用时会感到困惑,而在Python中,您突然需要传递它.因此,您不应盲目相信在互联网上找到的示例(例如 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.

这篇关于TypeError:尝试单击“日期选择器"时,无法调用"str"对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆