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?
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.
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.
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.
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()
Then you can try something like this.
for (int i=0; i<td.length(); i++){ driver.find_element_by_xpath("(//a[contains(text(),'Details')])[i]").click() }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With