python - Getting unexpected TypeErrorwhen trying to find element on page by text
问题描述
I am using a wait function to make Selenium search for the presence of an element on the page by its text before it continues to take actions. I am trying to pass the text that I am searching for into my function, however, I keep getting "TypeError: 'str' object is not callable", which is odd because I am looking for a string! I've tried everything I can think of, even escape sequences, but nothing seems to make it work. Please find my code below:
self.webdriver_wait(action = 'find_text', keys_to_pass = 'Financial Documents')
def webdriver_wait(self, selector = '#body', action='', keys_to_pass=''):
try:
WebDriverWait(self.browser, 240).until(EC.presence_of_element_located((By.XPATH("//*[contains(text(), \' + str(keys_to_pass) + \'')]"))))
解决方案
By.XPATH
不是一个方法,而是一个简单的字符串(By.XPATH == 'xpath'
),你需要使用它如下:
WebDriverWait(self.browser, 240).until(EC.presence_of_element_located((By.XPATH, "//*[contains(text(), '%s')]" % keys_to_pass)))
推荐阅读
- python-3.x - 我正在尝试使用 selenium/python 从具有 ::before 选择器的标签中提取文本
- javascript - 不断收到错误,因为 getElementById 返回 null
- excel - 未保存的 excel 文件 VB.NET VSTO 的问题
- excel - 如何使用 EXCEL VBA 中的宏发送 Outlook 邮件?
- c++ - 即使球体在场景中滚动,PhysX getLocalPose 也会返回 (0,0,0)
- python - A2C 代理在简单的 2D 网格世界中不学习任何东西
- html - 在两个堆叠的 svg 图像之间创建空间
- json - 如何将 base64 解码为字符串 Swift
- javascript - 无法控制台记录对象的属性
- uitableview - 表视图单元格重用打破了表视图单元格中的 FSCalendar