2

Is there a way to click on specific anchor tags with href BUT only with specific css selectors/text/tags/etc?

This is the link which I want to click through: http://www.oddsportal.com/soccer/germany/bundesliga/

NOTE: The table is table#tournamentTable.table-main, the one in the middle.

There is a table and I would like to click in each link displayed. (I already know there's something called 'is_displayed' but I haven't arrived to this issue yet so this is not the main problem)

The problem is that, as far as I have tried, I haven't figured out how to click only on the ones which I want! (Which have a unique css selector)

Here is my current code (I will point out where I am struggling):

#Load Modules
from selenium import webdriver
from time import sleep
from random import randint

#Set profile
profile = webdriver.FirefoxProfile()
profile.set_preference("Set Profile")

#Open driver
driver = webdriver.Firefox(profile)
driver.get("http://www.oddsportal.com/soccer/germany/bundesliga/")


#Find the elements (FIRST IDEA)
list_links=driver.find_elements_by_css_selector(".name.table-participant")
"""
These ones are not clickable BUT this is the list that I want.
"""
#Print
for i in list_links:
    print(i.text)


#(SECOND IDEA)
list_links=driver.find_elements_by_partial_link_text('-')
"""
Pretty close I guess, but there are still some 
elements that I don't want
"""
#Print
for i in list_links:
    print(i.text)


#(THIRD IDEA(S)) None of them works
list_links=driver.find_elements_by_xpath("//a[contains(text(),'/bundesliga/')]")
list_links=driver.find_element_by_xpath('//a[@href=soccer/germany/bundesliga/]')
list_links=driver.find_elements_by_css_selector(".name.table-participant[class='href']")
"""
I guess I'm pretty close but I can't get the click!
"""
#Print
for i in list_links:
    print(i.text)


#End

By the way, I stumbled upon with this: selenium click on anchor tag inside table td

I know is something related to xpath but...how to combine xpath which includes specific css selectors and also the anchor clickable tags?

Any thoughts?

2
  • 1
    You mentioned about specific anchor tags with href BUT only with specific css selectors/text/tags/ but you didn't mention which table/frame/portion_of_webpage. How can we know anchor tags from which table/frame/portion_of_webpage are you targeting? Commented Jan 31, 2018 at 9:44
  • @DebanjanB thanks for let me know! I've updated the post and the table is table#tournamentTable.table-main Commented Jan 31, 2018 at 17:23

1 Answer 1

1

You can use below line to get all team pairs:

 matches = [pair.text for pair in driver.find_elements_by_css_selector("td>a[href^='/soccer/germany/bundesliga/']") if pair.text]

Output:

['FC Koln - Dortmund', 'Freiburg - Bayer Leverkusen', 'Hertha Berlin - Hoffenhei
m', 'Mainz - Bayern Munich', 'Schalke - Werder Bremen', 'Wolfsburg - Stuttgart',
 'B. Monchengladbach - RB Leipzig', 'Augsburg - Eintracht Frankfurt', 'Hamburger
 SV - Hannover', 'RB Leipzig - Augsburg', 'Bayer Leverkusen - Hertha Berlin', 'D
ortmund - Hamburger SV', 'Eintracht Frankfurt - FC Koln', 'Hannover - Freiburg',
 'Hoffenheim - Mainz', 'Bayern Munich - Schalke', 'Stuttgart - B. Monchengladbac
h', 'Werder Bremen - Wolfsburg']

There are some hidden links in table. If you want to get those links also, try:

matches = [pair.get_attribute('textContent') for pair in driver.find_elements_by_css_selector("td>a[href^='/soccer/germany/bundesliga/']")]
Sign up to request clarification or add additional context in comments.

1 Comment

Amazing those are clickable! Thank you!

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.