在Python中使用Selenium,如何单击列表中每个项目的不同链接?

问题描述 投票:1回答:1

我在python中使用硒,并尝试单击链接,但是对于列表中的每个项目,其链接都不同。如果每小时更改一次,如何单击下面的链接?

<td class="name table-participant" colspan="2"><a 
href="/basketball/europe/euroleague/lyon-villeurbanne-alba-berlin- 
vcATLt3c/"><span class="bold">Lyon-Villeurbanne</span> - Alba 
Berlin</a></td>

driver = webdriver.Chrome()
driver.implicitly_wait(2)
driver.maximize_window()
driver.get("https://www.oddsportal.com")

elem = driver.find_element_by_link_text('BASKETBALL')
elem.click()
sleep(2)
elem1 = driver.find_element_by_link_text('Europe')
elem1.click()
sleep(2)
elem2 = driver.find_element_by_link_text('Euroleague')
elem2.click()
sleep(2)
elem3 = driver.find_element_by_link_text('RESULTS')
elem3.click()
sleep(2)

elem4 = driver.find_elements_by_xpath("td/a href[contains(text(), '/basketball/europe/euroleague/')]")
#WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.XPATH("//li[contains(., '/basketball/europe/euroleague/')"))))
elem4.click()
python selenium web-scraping tags
1个回答
0
投票

诱导WebDriverWait()和visibility_of_all_elements_located()并跟随CSS选择器以获取结果表中的所有元素。

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

driver=webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.oddsportal.com")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.LINK_TEXT,"BASKETBALL"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.LINK_TEXT,"Europe"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.LINK_TEXT,"Euroleague"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.LINK_TEXT,"RESULTS"))).click()

#To get all the elements
allelements=WebDriverWait(driver,15).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"td.name.table-participant >a[href^='/basketball/europe/euroleague/']")))

for i in range(len(allelements)):

    #To avoid stale exceptions
    allelements = WebDriverWait(driver, 15).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "td.name.table-participant >a[href^='/basketball/europe/euroleague/']")))
    print(allelements[i].text)
    #To avoid ElementClickInterceptedException 
    driver.execute_script("arguments[0].click();",  allelements[i])
    #Perform your opearions
    driver.back()
© www.soinside.com 2019 - 2024. All rights reserved.