Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking on a link via selenium

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?

like image 223
canbaran Avatar asked Sep 03 '13 17:09

canbaran


People also ask

How do you inspect a link in Selenium?

To check broken links in Selenium, the process is simple. On a web page, hyperlinks are implemented using the HTML Anchor (<a>) tag. All the script needs to do is to locate every anchor tag on a web page, get the corresponding URLs, and run through the links to check if any of them are broken.

How do I click a tag in Selenium?

We can click a href link with Selenium webdriver. There are multiple ways to achieve this. First of all we need to identify the link with the help of the locators like link text and partial link text. The link text locator identifies the element whose text matches with text enclosed within the anchor tag.

What are the ways to click in Selenium?

We can use the JavaScript Executor to perform a click action. Selenium can execute JavaScript commands with the help of the executeScript method. The parameters – arguments[0]. click() and locator of the element on which the click is to be performed are passed to this method.


2 Answers

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() 
like image 100
falsetru Avatar answered Oct 01 '22 22:10

falsetru


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()         } 
like image 21
Paras Avatar answered Oct 01 '22 22:10

Paras