繁体   English   中英

使用 Selenium Python 抓取并单击按钮

[英]Grab and Click Button using Selenium Python

在这个网站的“SELECT SIZE”下有几个尺寸选项: https://www.myntra.com/sports-shoes/puma/puma-men-black-eternity-nitro-running-shoes/14521968/buy

我一直在尝试使用 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'
        )

这是上述代码之一的错误:

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"]"}

要单击大小6 ,您需要为element_to_be_clickable()引入WebDriverWait ,您可以使用以下定位器策略

  • 使用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()
  • 注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
  • 浏览器快照:

myntra6

对于单击按钮,此代码可能会对您有所帮助

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()

注意1:您可以使用例如buttons[0].click() ,但按钮可能未加载,给您一个错误

注意2:在这个例子中,我使用了 class 作为选择器,但是您有无数的选择器选项(将来,随着经验的增加,您将能够选择最清晰的选择器!)

暂无
暂无

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

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