在Python中使用Selenium点击按钮时出错。

huangapple go评论85阅读模式
英文:

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: &#39;str&#39; object is not callable,

this is the code I executed

for i in soup.find_all(&#39;div&#39;, class_=&#39;ShopBrandsAlphabetically-brandLink&#39;):
    title = i.find(&#39;a&#39;, href = True)
    results.append(title.text.strip())
    time.sleep(2)
    driver.find_element(By.XPATH(&quot;//button[@name=&#39;0-9&#39;]&quot;)). click()

and this is the webpage element

&lt;button name=&quot;0-9&quot; value=&quot;0-9&quot; type=&quot;button&quot; data-hb-id=&quot;Button&quot; class=&quot;_1vl5e1z1_713 _1vl5e1z2_713 _1vl5e1z6_713 _1vl5e1z9_713 _1vl5e1z11_713&quot;&gt;&lt;span class=&quot;_1vl5e1zd_713 _1vl5e1z12_713 _1vl5e1z16_713 _1vl5e1z18_713&quot;&gt;&lt;span class=&quot;ShopBrandsAlphabetically-filter&quot;&gt;&lt;p data-hb-id=&quot;Text&quot; class=&quot;j3rpje110_713 nhya890_713&quot; style=&quot;--j3rpje10w_713:inherit&quot;&gt;0-9&lt;/p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/button&gt;

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, &quot;//button[@name=&#39;0-9&#39;]&quot;)

But instead you did this:

driver.find_element(By.XPATH(&quot;//button[@name=&#39;0-9&#39;]&quot;))

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 &quot;//button[@name=&#39;0-9&#39;]&quot;.

Because you are putting By.XPATH(&quot;//button[@name=&#39;0-9&#39;]&quot;) inside the type parameter, selenium thinks you are tying to click By.XPATH(&quot;//button[@name=&#39;0-9&#39;]&quot;), which is a string and not the button you are attempting to access.

In conclusion, try using this:

driver.find_element(By.XPATH,&quot;//button[@name=&#39;0-9&#39;]&quot;).click()

This should get the button you are tying to access.

英文:

I believe you have to put a comma between By.XPATH and &quot;//button[@name=&#39;0-9&#39;]&quot;.

Because you are putting By.XPATH(&quot;//button[@name=&#39;0-9&#39;]&quot;) inside the type parameter, selenium thinks you are tying to click By.XPATH(&quot;//button[@name=&#39;0-9&#39;]&quot;), which is a string and not the button you are attempting to access.

In conclusion, try using this:

driver.find_element(By.XPATH,&quot;//button[@name=&#39;0-9&#39;]&quot;).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) -&gt; WebElement:
    &quot;&quot;&quot;Find an element given a By strategy and locator.

    :Usage:
        ::

            element = driver.find_element(By.ID, &#39;foo&#39;)

    :rtype: WebElement
    &quot;&quot;&quot;

Solution

In your usecase instead of function you need to pass as a tuple as follows:

driver.find_element(By.XPATH, &quot;//button[@name=&#39;0-9&#39;]&quot;).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, &quot;button[name=&#39;0-9&#39;][value=&#39;0-9&#39;] p&quot;))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, &quot;//button[@name=&#39;0-9&#39; and @value=&#39;0-9&#39;]//p[text()=&#39;0-9&#39;]&quot;))).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
    

huangapple
  • 本文由 发表于 2023年7月18日 02:57:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定