[英]Selenium (browser.find_element(by=By.PARTIAL_LINK_TEXT) error
I need to find a partial link button on the page and click on it.我需要在页面上找到一个部分链接按钮并单击它。 But it gives me an error.
但这给了我一个错误。
Code:代码:
button = browser.find_element(by=By.PARTIAL_LINK_TEXT('/watch')).click()
Error:错误:
button = browser.find_element(by=By.PARTIAL_LINK_TEXT('/watch')).click()
TypeError: 'str' object is not callable
There are 2 problems here:这里有2个问题:
browser.find_element(by=By.PARTIAL_LINK_TEXT('/watch'))
it should be它应该是
browser.find_element(By.PARTIAL_LINK_TEXT('/watch'))
button
object.button
object。 Because browser.find_element(By.PARTIAL_LINK_TEXT('/watch'))
returns an object of web element so you can click on it with .click()
method.browser.find_element(By.PARTIAL_LINK_TEXT('/watch'))
返回 web 元素的 object ,所以您可以使用 .click .click()
方法单击它。 However .click()
method itself returns void ie nothing..click()
方法本身返回 void 即什么也没有。 So your code can bebrowser.find_element(By.PARTIAL_LINK_TEXT('/watch')).click()
or或者
button = browser.find_element(By.PARTIAL_LINK_TEXT('/watch'))
button.click()
This is the wrong way这是错误的方式
button = browser.find_element(by=By.PARTIAL_LINK_TEXT('/watch')).click()
You should use this:你应该使用这个:
button = browser.find_element(By.PARTIAL_LINK_TEXT, "/watch")
button.click()
Also, If You see the method signature is此外,如果您看到方法签名是
def find_element(self, by=By.ID, value=None):
It is expected to pass By
then separate that with the actual locator.预计将通过
By
然后将其与实际定位器分开。
Also, I would recommend having WebDriverWait (ExplicitWait) for this use case.另外,我建议为这个用例使用 WebDriverWait (ExplicitWait)。
button = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "/watch")))
button.click()
You will have to import these as well.您还必须导入这些。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You were close enough.你离得够近了。 The actual implementation is:
实际的实现是:
self.find_element(by=By.PARTIAL_LINK_TEXT, value=link_text)
So you need to pass two parameters,所以你需要传递两个参数,
The by_ parameter
: by_ parameter
:
by=By.PARTIAL_LINK_TEXT
The value
: value
:
/watch
So your effective line of code will be:因此,您的有效代码行将是:
browser.find_element(By.PARTIAL_LINK_TEXT, "/watch")
Finally, click()
doesn't returns anything, so if to try to assign it to a variable, it will be always NULL
.最后,
click()
不返回任何内容,因此如果尝试将其分配给变量,则始终NULL
。 So it's better to avoid it.所以最好避免它。
So your working line of code will be:所以你的工作代码行将是:
browser.find_element(By.PARTIAL_LINK_TEXT, "/watch").clcik()
Ideally to invoke a click()
you need to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategy :理想情况下,要调用
click()
,您需要为element_to_be_clickable()
诱导WebDriverWait ,并且可以使用以下任一Locator Strategy :
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "/watch"))).click()
Note : You have to add the following imports:注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.