简体   繁体   English

使用 Selenium Python 抓取并单击按钮

[英]Grab and Click Button using Selenium Python

There are several size options under 'SELECT SIZE' on this site: https://www.myntra.com/sports-shoes/puma/puma-men-black-eternity-nitro-running-shoes/14521968/buy在这个网站的“SELECT SIZE”下有几个尺寸选项: https://www.myntra.com/sports-shoes/puma/puma-men-black-eternity-nitro-running-shoes/14521968/buy

I have been trying to somehow click one button to choose size of the shoes using python selenium but every time it says "no such element: unable to locate element:.....".Here's what I have tried till now.我一直在尝试使用 python selenium 以某种方式单击一个按钮来选择鞋子的尺寸,但每次它都说“没有这样的元素:无法找到元素:.....”。这是我到目前为止所尝试的。

size_button = self.find_element(
            By.CSS_SELECTOR,
            'button[class="btn default outline   size-btn big selected"]'
        )
size_button = self.find_element(
            By.CSS_SELECTOR,
            'ul[class="sizes-list list-unstyled list-inline padding-md-left padding-md-bottom"]'
        )
size_button = self.find_element(
            By.XPATH,            '/html/body/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/div[1]/div/div/div[4]/div[6]/div[2]/div[1]/div[2]/ul/li[1]/button'
        )
size_button = self.find_element(
            By.XPATH,           '//[@id="reactPageContent"]/div/div/div[4]/div[6]/div[2]/div[1]/div[2]/ul/li[1]/button'
        )

Here's the error for one of the above code:这是上述代码之一的错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button[class="btn default outline   size-btn big selected"]"}

To click on Size 6 you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy :要单击大小6 ,您需要为element_to_be_clickable()引入WebDriverWait ,您可以使用以下定位器策略

  • Using XPATH :使用XPATH

     driver.get('https://www.myntra.com/sports-shoes/puma/puma-men-black-eternity-nitro-running-shoes/14521968/buy') WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='size-buttons-size-button size-buttons-size-button-default size-buttons-big-size']//p[text()='6']"))).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
  • Browser Snapshot:浏览器快照:

myntra6

For click on buttons this code may help you对于单击按钮,此代码可能会对您有所帮助

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

import time

from selenium.webdriver.support.wait import WebDriverWait

if __name__ == '__main__':
    options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(executable_path="chromedriver", chrome_options=options)

    driver.get('https://www.myntra.com/sports-shoes/puma/puma-men-black-eternity-nitro-running-shoes/14521968/buy')

    # get all buttons by common class
    buttons = driver.find_elements(By.CLASS_NAME, 'size-buttons-size-button')

    # wait for first button (buttons[0]) be clickable and click
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable(buttons[0])).click()
    time.sleep(3)
    
    # clicking in all buttons
    for button in buttons:
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable(button)).click()
        time.sleep(3)

    driver.quit()

note1: you can use for example buttons[0].click() but the button may not loaded, giving for you a error注意1:您可以使用例如buttons[0].click() ,但按钮可能未加载,给您一个错误

note2: in this example i used the class as selector but you have countless selector options (in future, with more experience, you will be able to choose the clearest selector!)注意2:在这个例子中,我使用了 class 作为选择器,但是您有无数的选择器选项(将来,随着经验的增加,您将能够选择最清晰的选择器!)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM