英文:
Error with clicking button using selenium in python
问题
I was trying to scrape this URL and trying to navigate the next Brand list on the website. I use selenium click by.XPATH and got the error TypeError: 'str' object is not callable
,
this is the code I executed
for i in soup.find_all('div', class_='ShopBrandsAlphabetically-brandLink'):
title = i.find('a', href=True)
results.append(title.text.strip())
time.sleep(2)
driver.find_element(By.XPATH("//button[@name='0-9']").click()
and this is the webpage element
<button name="0-9" value="0-9" type="button" data-hb-id="Button" class="_1vl5e1z1_713 _1vl5e1z2_713 _1vl5e1z6_713 _1vl5e1z9_713 _1vl5e1z11_713"><span class="_1vl5e1zd_713 _1vl5e1z12_713 _1vl5e1z16_713 _1vl5e1z18_713"><span class="ShopBrandsAlphabetically-filter"><p data-hb-id="Text" class="j3rpje110_713 nhya890_713" style="--j3rpje10w_713:inherit">0-9</p></span></span></button>
can anyone tell me where's my mistake?
英文:
I was trying to scrape this URL and trying to navigate the next Brand list on the website. I use selenium click by.XPATH and got the error TypeError: 'str' object is not callable
,
this is the code I executed
for i in soup.find_all('div', class_='ShopBrandsAlphabetically-brandLink'):
title = i.find('a', href = True)
results.append(title.text.strip())
time.sleep(2)
driver.find_element(By.XPATH("//button[@name='0-9']")). click()
and this is the webpage element
<button name="0-9" value="0-9" type="button" data-hb-id="Button" class="_1vl5e1z1_713 _1vl5e1z2_713 _1vl5e1z6_713 _1vl5e1z9_713 _1vl5e1z11_713"><span class="_1vl5e1zd_713 _1vl5e1z12_713 _1vl5e1z16_713 _1vl5e1z18_713"><span class="ShopBrandsAlphabetically-filter"><p data-hb-id="Text" class="j3rpje110_713 nhya890_713" style="--j3rpje10w_713:inherit">0-9</p></span></span></button>
can anyone tell me where's my mistake?
答案1
得分: 1
你使用 By.XPATH
错误了。
应该这样调用:
driver.find_element(By.XPATH, "//button[@name='0-9']")
但是你做了这样的调用:
driver.find_element(By.XPATH("//button[@name='0-9']"))
紧跟在 By.XPATH(...)
后面的括号表明你将其视为一个函数。但它不是一个函数。
英文:
You're using By.XPATH
wrongly.
The call should be this:
driver.find_element(By.XPATH, "//button[@name='0-9']")
But instead you did this:
driver.find_element(By.XPATH("//button[@name='0-9']"))
The parentheses immediately following By.XPATH(...)
indicate that you're treating it as a function. But it is not a function.
答案2
得分: 0
I believe you have to put a comma between By.XPATH
and "//button[@name='0-9']"
.
Because you are putting By.XPATH("//button[@name='0-9']")
inside the type parameter, selenium thinks you are tying to click By.XPATH("//button[@name='0-9']")
, which is a string and not the button you are attempting to access.
In conclusion, try using this:
driver.find_element(By.XPATH,"//button[@name='0-9']").click()
This should get the button you are tying to access.
英文:
I believe you have to put a comma between By.XPATH
and "//button[@name='0-9']"
.
Because you are putting By.XPATH("//button[@name='0-9']")
inside the type parameter, selenium thinks you are tying to click By.XPATH("//button[@name='0-9']")
, which is a string and not the button you are attempting to access.
In conclusion, try using this:
driver.find_element(By.XPATH,"//button[@name='0-9']").click()
This should get the button you are tying to access.
答案3
得分: 0
根据find_element()
的定义,它根据By
策略和定位器查找元素,定义如下:
def find_element(self, by=By.ID, value: Optional[str] = None) -> WebElement:
"""根据By策略和定位器查找元素。
:Usage:
::
element = driver.find_element(By.ID, 'foo')
:rtype: WebElement
"""
解决方案
在您的用例中,您需要将function
更改为tuple,如下所示:
driver.find_element(By.XPATH, "//button[@name='0-9']").click()
然而,所需的元素是启用JavaScript的元素,因此要点击可点击的元素,您需要使用WebDriverWait来等待element_to_be_clickable(),并且可以使用以下任一定位策略:
- 使用_CSS_SELECTOR_:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[name='0-9'][value='0-9'] p"))).click()
- 使用_XPATH_:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@name='0-9' and @value='0-9']//p[text()='0-9']"))).click()
- 注意:您需要添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
英文:
As per the defination find_element()
finds an element given a By
strategy and a locator and is defined as:
def find_element(self, by=By.ID, value: Optional[str] = None) -> WebElement:
"""Find an element given a By strategy and locator.
:Usage:
::
element = driver.find_element(By.ID, 'foo')
:rtype: WebElement
"""
Solution
In your usecase instead of function
you need to pass as a tuple as follows:
driver.find_element(By.XPATH, "//button[@name='0-9']").click()
However the desired element is a JavaScript enabled element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
-
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[name='0-9'][value='0-9'] p"))).click()
-
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@name='0-9' and @value='0-9']//p[text()='0-9']"))).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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论