45

I am trying to do some webscraping via Selenium. My question is very simple: How do you find a link and then how do you click on it? For instance: The following is the HTML that I am trying to web-scrape:

<td bgcolor="#E7EFF9">
  <a href="javascript:selectDodasaDetdasdasy(220011643,'Kdasdası');" target="_self">
   Details
  </a>
</td>

So, as you can see the word "Details" is a link.

How can I find that link using Selenium and click on it?

petezurich
  • 9,280
  • 9
  • 43
  • 57
canbaran
  • 465
  • 1
  • 4
  • 7
  • 1
    I tried the suggested idea on a td_element after I got the td element via td_tag = tr_tag.find_element_by_tag_name('td'). When I call the find_element_by_link_text('details') I get the selenium.common.exceptions.NoSuchElementException error with the message unable to locate element locate element {''method'': ''link text'', ''selector'': ''details''}' – canbaran Sep 04 '13 at 15:57

4 Answers4

77

You can use find_element_by_link_text:

For example:

link = driver.find_element_by_link_text('Details')

To Click on it, just call click method:

link.click()
Derek Adair
  • 21,846
  • 31
  • 97
  • 134
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 1
    so thanks, but how can I call on that on a td object like the above ? For instance I have elem_query_result = driver.find_element_by_id('sorgudadasdsac') then I have tr_tag= elem_query_result.find_element_by_tag_name('tr') then I have td_tag = tr_tag.find_element_by_tag_name('td') and I want to call find_element_by_link_text('Details') on the td_tag, but it gives me the following error: unable to locate element {''method'': ''link text'', ''selector'': ''details''}' – canbaran Sep 04 '13 at 15:54
  • @canbaran, once you find your element, you can call again the function over it. In example, once you do `td_tag = tr_tag.find_element_by_tag_name('td')`, you can do `td_tag.find_elements_by_tag_name("a")` to find links on it. – m3nda Jul 29 '19 at 06:55
2

Then you can try something like this.

for (int i=0; i&lttd.length(); i++){
        driver.find_element_by_xpath("(//a[contains(text(),'Details')])[i]").click()
        }
petezurich
  • 9,280
  • 9
  • 43
  • 57
Paras
  • 3,197
  • 2
  • 20
  • 30
  • cant I call find_element_by_link on a td tag? – canbaran Sep 04 '13 at 16:29
  • there are many links with the text ''details'', I am looping over the tags and when I am inside a td, I want to call find_element_by_link_text on that td tag. does that make sense ? – canbaran Sep 04 '13 at 16:31
  • I did try and I appreciate it but it gives all the links. As said, I am looping over them, I want to be able to open each one through the loop – canbaran Sep 04 '13 at 16:32
  • see if there are many links with the text "details", then by link_text you won't be able to find its unique property, you need to search for a unique identifier. – Paras Sep 04 '13 at 16:32
  • check I've modified my answer, put this line of code in a for loop and iterate on variable 'i' it should work fine then. – Paras Sep 04 '13 at 16:41
  • I see thanks, but specifically cant you call this find_element_by_link on a td_tag ? when I do that I pasted the error I get – canbaran Sep 04 '13 at 16:46
  • I am sorry but you are not exactly answering my question. I will go ahead and accept the original answer and make it plural driver.find_element'S'_by_link_text and change the way I tackle my problem. We can start a different thread if desired or continue the discussion over emails – canbaran Sep 04 '13 at 16:57
0

You can try to click link by using xpath locator e.g.

link=driver.find_element_by_xpath(.//*[@id="content"]/div[3]/div/div/div[2]/h4)

link.click()
JJJ
  • 32,902
  • 20
  • 89
  • 102
Gayatri
  • 11
  • 2
-2

One thing is missed by everyone. Its a list by the below statement. You need to take select an element from this list.

driver.find_element_by_link_text('Details')

If you check

for i in driver.find_element_by_link_text('Details')
    i.click()

BINGO :-)

petezurich
  • 9,280
  • 9
  • 43
  • 57
Mr. Bordoloi
  • 80
  • 11
  • 1
    Have you tried your own code? First, `find_by_element` returns a webdriver element, in case you try to click everything iterating it, you'll surely get error because strings or others doesn't have the click attribute. Maybe you are talking about `find_elements` (note the s, plural), so you get a list of webdriver elements that you can click() – m3nda Jul 29 '19 at 06:52
  • 2
    @petezurich, what's the purpose of editing something that is wrong? – m3nda Jul 29 '19 at 06:53
  • 1
    @erm3nda To improve overall site quality? To give any answer a fair chance of feedback e.g. the specific and with good reason critical feedback that you kindly provided? – petezurich Jul 30 '19 at 07:11