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

在Python中使用Selenium,如何单击列表中每个项目的不同链接?,python,selenium,web-scraping,tags,Python,Selenium,Web Scraping,Tags,我在python中使用selenium并尝试单击链接,但是列表中的每个项目都不同。如果每小时都有更改,如何单击下面的链接 <td class="name table-participant" colspan="2"><a href="/basketball/europe/euroleague/lyon-villeurbanne-alba-berlin- vcATLt3c/"><span class="bold">Lyon-Villeurbanne</

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

<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()

driver=webdriver.Chrome()
驱动程序。隐式等待(2)
驱动程序。最大化_窗口()
驱动程序。获取(“https://www.oddsportal.com")
elem=驱动程序。通过链接文本(“BASKETBALL”)查找元素
元素单击()
睡眠(2)
elem1=驱动程序。通过链接文本(“欧洲”)查找元素
elem1.click()
睡眠(2)
elem2=驱动程序。通过链接文本(“欧洲联盟”)查找元素
elem2.click()
睡眠(2)
elem3=驱动程序。通过链接文本(“结果”)查找元素
elem3.click()
睡眠(2)
elem4=driver.find_elements_by_xpath(“td/a href[contains(text(),'/basketball/europe/euroleague/')]))
#WebDriverWait(driver,10).until(EC.presence_所有元素的位置((By.XPATH(//li[contains(,,'/basketball/europe/euroleague/'))))
elem4.单击()

归纳
WebDriverWait
()和
所有元素的可见性(位于
())并遵循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()

您必须使选择条件更加稳定。例如,您可以使用通过文本获取链接。@AndreySemakin驱动程序。通过xpath(“li[contains(text(),”/basketball/european/euroleague/“)查找元素”)…我需要更具体的东西太多这样的链接当你通过浏览器使用网站时,你如何选择正确的链接?@devlops_s你发布的代码按预期工作,并将元素列表返回给
elem4
变量。你到底需要哪个链接?首先,任何或某些特定的链接?进入Iam页面后,查看cli在选项卡上单击……
  • 亚洲障碍”
  • .ebDriverWait(驱动程序,20)。直到(EC.element\u可点击(.CSS\u选择器,亚洲障碍”)。单击()给我一个错误您的CSS选择器错误。尝试使用以下XPATH
    WebDriverWait(驱动程序,10)。直到(EC.element可点击((By.XPATH,//span[text()='Asian disability']))。点击()
    你确定页面上存在该元素吗。如果你能发布一个新问题并发布你的工作流程,那就太好了。谢谢这是我面临的页面问题,如何循环页面。也许你可以帮助。。。。。