简体   繁体   English

“TypeError: 'str' object is not callable” 使用 WebDriverWait 处理 Selenium 到 Python 中的 link_text

[英]“TypeError: 'str' object is not callable” using WebDriverWait for link_text in Selenium through Python

This is my first post on Stack Overflow.这是我在 Stack Overflow 上的第一篇文章。 I have been browsing and searching for every possible answer to this question on SO, and I figured at this point I should just ask a question, as I have been at this wall for days now.我一直在浏览和搜索关于 SO 的这个问题的所有可能答案,我想此时我应该问一个问题,因为我已经在这堵墙上好几天了。 I am currently working on a web scraping project with Selenium in Python.我目前正在使用 Python 中的 Selenium 进行 web 抓取项目。 I have already scraped one website and am currently on the second, and I have run into a seemingly intractable issue.我已经抓取了一个网站,目前正在第二个网站上,我遇到了一个看似棘手的问题。 The following code works perfectly fine with no errors on the page https://www.spd.de/standpunkte/ :以下代码在页面https://www.spd.de/standpunkte/上运行良好,没有错误:

try:
    driver.find_element_by_link_text("Familien").click()
except NoSuchElementException:
    WebDriverWait(driver, 12).until("Familien").click()

Meanwhile, the following code throws the error TypeError: 'str' object is not callable on the page https://www.spd.de/standpunkte/familie/ :同时,以下代码抛出错误TypeError: 'str' object is not callable on page https://www.spd.de/standpunkte/familie/

try:
    driver.find_element_by_link_text("Mehr erfahren").click()
except NoSuchElementException:
    WebDriverWait(driver, 12).until("Mehr erfahren").click()

The error occurs on the line WebDriverWait(driver, 12).until("Mehr erfahren").click() .错误发生在WebDriverWait(driver, 12).until("Mehr erfahren").click()行上。 Based on my research up to now, I figure the specific issue is that something in this line is being interpreted as a str object, and is therefore unable to be called.根据我到目前为止的研究,我认为具体问题是该行中的某些内容被解释为str object,因此无法调用。 There are, as I see it, two issues:在我看来,有两个问题:

  1. I don't see any reason for this line to produce the error while the code on top, scraping the same website, but on a different page, works without issue.我看不出有任何理由让这条线产生错误,而顶部的代码,抓取同一个网站,但在不同的页面上,没有问题。 I checked and made sure there are no hidden elements on the page that are being clicked instead.我检查并确保页面上没有被点击的隐藏元素。

  2. I don't know what in the line is being treated as a str object.我不知道该行中的什么被视为str object。 The whole line?整条线? Only part of it?只有一部分? If so, which part?如果有,是哪一部分? Exacerbating this is that I can't really break down the line to see anything about it;加剧这一点的是,我无法真正打破界限以查看有关它的任何信息。 both print(WebDriverWait(driver, 12).until("Mehr erfahren").click()) and print(type(WebDriverWait(driver, 12).until("Mehr erfahren").click())) just end up giving the error. print(WebDriverWait(driver, 12).until("Mehr erfahren").click())print(type(WebDriverWait(driver, 12).until("Mehr erfahren").click()))都结束了给出错误。 I was able to determine that the .click() method is not the issue, as I tried setting the line to be equal to a variable, and then interacted with the variable, but that still produced the error on the same line as before;我能够确定.click()方法不是问题,因为我尝试将行设置为等于变量,然后与变量进行交互,但这仍然在与以前相同的行上产生错误; it never even got to the line with the .click() method.它甚至从未与.click()方法达成一致。

Is there any way I can determine which part of the line is being interpreted as a str ?有什么方法可以确定该行的哪一部分被解释为str Or is that even the issue at all?或者这根本就是问题所在?

This has been driving me crazy, so assistance would be greatly appreciated!这一直让我发疯,因此将不胜感激!

I think I found your problem here .我想我在这里找到了你的问题。 From the source从源头

def until(self, method, message=''):
    ...

until takes a method (ie something callable). until接受一个方法(即可调用的东西)。 So, when driver.find_element_by_link_text("Mehr erfahren").click() raises an exception, it will attempt to call ...until("Mehr erfahren")... .因此,当driver.find_element_by_link_text("Mehr erfahren").click()引发异常时,它将尝试调用...until("Mehr erfahren")... Since until expects a method it will try to call the string you gave it - raising the error that's been driving you crazy:)因为until期望一个方法,它会尝试调用你给它的字符串——引发让你发疯的错误:)

I think until expects a conditional (and WebDriverWait waits until that condition is true).我认为直到需要一个条件(并且 WebDriverWait 等到该条件为真)。 You're just passing a string as your argument instead of a condition.您只是将字符串作为参数而不是条件传递。 You need a condition that checks if that element exists on the page.您需要一个条件来检查页面上是否存在该元素。

You can use this css selector: a[href*="familien"]您可以使用此 css 选择器: a[href*="familien"]

This can be used for both your URLs.这可用于您的两个 URL。

driver.get('yourURL')
try:
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[href*="familien"]'))).click()
except NoSuchElementException:
    # handle if the element not exist
    print('element not exist')

See documentation here to know how to use WebDriverWait请参阅此处的文档以了解如何使用WebDriverWait

Your research was in the right direction.你的研究方向是对的。

In your first usecase, the line within try was successful:在您的第一个用例中, try中的行是成功的:

driver.find_element_by_link_text("Familien").click()

So the line within except was never called:因此, except中的行从未被调用:

WebDriverWait(driver, 12).until("Familien").click()

Hence you don't see the error.因此,您看不到错误。


In your second use-case, the line within try wasn't successful:在您的第二个用例中, try中的行不成功:

driver.find_element_by_link_text("Familien").click()

So the line within except was called:所以里面的except被称为:

WebDriverWait(driver, 12).until("Mehr erfahren").click()

which results into the error:这导致错误:

TypeError: 'str' object is not callable

Deep Dive深潜

WebDriverWait is invoked inconjunction with expected_conditions . WebDriverWaitexpected_conditions结合调用。

You can find a couple of detailed discussions in:您可以在以下位置找到一些详细的讨论:

Now, element_to_be_clickable() should be called within a tuple as it is not a function but a class , where the initializer expects just 1 argument beyond the implicit self :现在, element_to_be_clickable()应该在tuple中调用,因为它不是function而是class ,其中初始化程序只需要隐式self之外的1 个参数:

class element_to_be_clickable(object):
    """ An Expectation for checking an element is visible and enabled such that you can click it."""
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        element = visibility_of_element_located(self.locator)(driver)
        if element and element.is_enabled():
            return element
        else:
            return False

So instead of:所以而不是:

WebDriverWait(driver, 12).until("Mehr erfahren").click()

You need to (add an extra parentheses):您需要(添加额外的括号):

WebDriverWait(driver, 12).until((EC.element_to_be_clickable(By.LINK_TEXT, "Mehr erfahren"))).click()

You can find a detailed discussions in init () takes 2 positional arguments but 3 were given using WebDriverWait and expected_conditions as element_to_be_clickable with Selenium Python您可以在init () 中找到详细讨论,其中包含 2 个位置 arguments 但使用 WebDriverWait 和 expected_conditions 作为 element_to_be_clickable 和 Selenium ZA7F5F35426B92682137

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:'str'对象不能通过Python使用Selenium调用 - TypeError: 'str' object is not callable using Selenium through Python TypeError: 'str' object is not callable error using text() from Selenium Python - TypeError: 'str' object is not callable error using text() from Selenium Python find_element() with Link_Text 无法使用 Selenium Python - find_element() with Link_Text not working using Selenium Python TypeError: 'str' object 在 python selenium 代码中不可调用 - TypeError: 'str' object is not callable in python selenium code TypeError: 'str' object is not callable error while print the text from a div tag using Selenium and Python - TypeError: 'str' object is not callable error while printing the text from a div tag using Selenium and Python python selenium - 类型错误:'str' object 不可调用 - python selenium - TypeError: 'str' object is not callable Selenium 网络驱动程序 Python | TypeError: 'str' object 不可调用 - Selenium webdriver Python | TypeError: 'str' object is not callable TypeError:“str”对象在 selenium python 中不可调用 - TypeError: 'str' object is not callable in selenium python 'str' object is not callable error while using 'Find an element' by link text or XPATH in Selenium Python - 'str' object is not callable error while using 'Find an element' by link text or XPATH in Selenium Python Selenium: TypeError: 'str' object 不可调用 - Selenium: TypeError: 'str' object is not callable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM