I am trying get all the rounds to be the same and the best i can do is change the first one. The current code has all the player scorecards open now i just need to be able to select the same round for all of them.
ignore all the imports i was trying a few things, i am extremely new to this
Thank you!
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup
import requests
PATH = "C:\Program Files (x86)\Chrome\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.espn.com/golf/leaderboard?tournamentId=401353233")
xpath_number = 1
while True:
try:
element = driver.find_element_by_xpath(f'//*[@id="fittPageContainer"]/div[3]/div/div/section[3]/div/div/div/div[2]/div[3]/div/div/div/div[2]/table/tbody/tr[{xpath_number}]/td[1]')
xpath_number = 1
action = ActionChains(driver)
action.click(element)
action.perform()
except NoSuchElementException:
break
CodePudding user response:
This code runs a first loop to open the scorecards, then a second loop to change the round. The command .send_keys(Keys.DOWN * n)
where n is an integer, press the down arrow n times.
import time
from selenium.webdriver.common.keys import Keys
number_of_players = 10
round_to_select = 1
for idx,down_arrow in enumerate(driver.find_elements(By.CSS_SELECTOR, '.Table__TD:first-child')):
if idx < number_of_players:
down_arrow.click()
time.sleep(.5)
else:
break
if round_to_select < 4:
for idx,menu in enumerate(driver.find_elements(By.CSS_SELECTOR, '.competitors select[class=dropdown__select]')):
if idx < number_of_players:
menu.send_keys(Keys.DOWN * (4-round_to_select))
time.sleep(.5)
else:
break
Alternatively to menu.send_keys(Keys.DOWN * (4-round_to_select))
we can also use Select
as follows
from selenium.webdriver.support.ui import Select
...
if round_to_select < 4:
for idx,menu in enumerate(driver.find_elements(By.CSS_SELECTOR, '.competitors select[class=dropdown__select]')):
if idx < number_of_players:
Select(menu).select_by_visible_text(f'Round {round_to_select}')
time.sleep(.5)
else:
break