0

Iam using selenium in python and trying click on a link however its different for every item in a list. How do I click the link below if changes each hour?

<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()
4
  • 1
    You have to make your selection criteria more stable. For example, you can use XPath to get a link by its text. Commented Dec 21, 2019 at 5:37
  • @AndreySemakin driver.find_elements_by_xpath("//li[contains(text(), '/basketball/europe/euroleague/')]") .... I need something more specific too many links like this Commented Dec 21, 2019 at 6:20
  • How do you choose correct link when you use the website via browser? Commented Dec 21, 2019 at 7:37
  • @devlops_s the code you posted works as expected and returns you a list of elements to elem4 variable. Which link exactly do you need? First, any or some particular? Commented Dec 21, 2019 at 7:57

1 Answer 1

1

Induce WebDriverWait() and visibility_of_all_elements_located() and following CSS selector to get all the elements in result table.

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()
Sign up to request clarification or add additional context in comments.

4 Comments

after getting into page Iam looking to click on a tab ......<li class=" active" style="display: block;"><span class="topleft_corner"></span><span class="topright_corner"></span><strong><span>Asian Handicap</span></strong></li> ........... ebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"Asian Handicap"))).click() ................is giving me a error
your css selector is wrong.try use following XPATH WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[text()='Asian Handicap']"))).click()
Are you sure that element present on the page.It would be great if you could post a new question and post your workflow as well.Thanks
this is the page issue iam facing how loop pages. Maybe you could help..... stackoverflow.com/questions/59471668/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.