英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论