按下按钮使用Selenium Python

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

Pressing Button with Selenium Python

问题

from selenium import webdriver
from selenium.webdriver.support.select import Select
import time
driver = webdriver.Chrome(executable_path = r'G:/scraping_practice/chromedriver_win32/chromedriver.exe')
driver.get('https://www.maxpreps.com/tx/basketball/21-22/stat-leaders/scoring/ppg/')

search_button = driver.find_element(By.xpath('/html/body/div[1]/div[4]/div[1]/div/div[2]/div[3]/div/div/ul/li[2]/button'))
search_button.click()
英文:

I am trying to press a button with selenium. The page comes up, but it does not press the button. I am new to this, and also the page as a GEO block for any one in the UK. I am using a windows 10 laptop.

This is the code I have so far:

from selenium import webdriver
from selenium.webdriver.support.select import Select
import time 
driver = webdriver.Chrome(executable_path = r'G:/scraping_practice/chromedriver_win32/chromedriver.exe')
driver.get('https://www.maxpreps.com/tx/basketball/21-22/stat-leaders/scoring/ppg/')

search_button = driver.find_element(By.xpath('/html/body/div[1]/div[4]/div[1]/div/div[2]/div[3]/div/div/ul/li[2]/button'))
search_button.click()

答案1

得分: 1

使用WebDriverWait来单击具有文本_2_的元素,您需要诱发WebDriverWait以等待element_to_be_clickable()并且可以使用以下定位策略

  • 使用_XPATH_:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='controls']//ul/li/button[text()='2']"))).click()

  • 注意:您需要添加以下导入:

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

英文:

To click on the element with text 2 you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:

  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='controls']//ul/li/button[text()='2']"))).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年2月16日 05:51:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465768.html
匿名

发表评论

匿名网友

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

确定