Home > OS >  Cannot make button click with Selenium in Python
Cannot make button click with Selenium in Python

Time:06-23

I am attempting to scrape a table of stats from mlb.com but need to click a button to expand the stats. When running my code, it appears that the button is hovered over, but nothing seems to actually click it and change the table. How can I expand the table to scrape the data?

Below is the code I have, slimmed down to isolate my problem.

driver = webdriver.Chrome('/home/wsb/Downloads/chromedriver',options = options)
wait = WebDriverWait(driver, 30)

url_mlbcom_h = 'https://www.mlb.com/stats/team'

driver.get(url_mlbcom_h)

button = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="stats-app-root"]/section/section/div[1]/div[2]/div/div[1]/div/div[2]/button'))).click()

CodePudding user response:

Button Rendering Issue:

This seems to be a strange "double-click" phenomenon to render the button. Other users have had this problem before:

Well, the first click function seems to make the button get focus, and the second click function active submit operation.

I was able to reproduce this issue and solve it by having the button command run twice:

button = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="stats-app-root"]/section/section/div[1]/div[2]/div/div[1]/div/div[2]/button'))).click()
button = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="stats-app-root"]/section/section/div[1]/div[2]/div/div[1]/div/div[2]/button'))).click()

CodePudding user response:

button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="stats-app-root"]/section/section/div[1]/div[2]/div/div[1]/div/div[2]/button')))
button.click()
button.click()

You can just click it twice and it does work. Also use element_to_be_clickable instead of visibility_of_element_located .

  • Related