【问题标题】:Using Selenium in Python, how do I click a link that is different for each item in a list?在 Python 中使用 Selenium,如何单击列表中每个项目的不同链接?
【发布时间】:2019-12-21 05:23:43
【问题描述】:

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

【问题讨论】:

  • 您必须使您的选择标准更加稳定。例如,您可以使用XPath 通过文本获取链接。
  • @AndreySemakin driver.find_elements_by_xpath("//li[contains(text(), '/basketball/europe/euroleague/')]") .... 我需要更具体的东西太多链接像这样
  • 通过浏览器使用网站时如何选择正确的链接?
  • @devlops_s 您发布的代码按预期工作,并将元素列表返回给elem4 变量。您究竟需要哪个链接?首先,有什么特别的吗?

标签: python selenium web-scraping tags


【解决方案1】:

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

【讨论】:

  • 进入页面后我想点击一个标签......
  • 亚洲盘
  • ......... ebDriverWait(司机, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"Asian Handicap"))).click() ......给我一个错误
  • 您的 css 选择器错误。请尝试使用以下 XPATH WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[text()='Asian Handicap']"))).click()
  • 您确定页面上存在该元素吗?如果您可以发布一个新问题并发布您的工作流程,那就太好了。谢谢
  • 这是我面临的页面问题如何循环页面。也许你可以帮忙.....stackoverflow.com/questions/59471668/…
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签