Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click a href button with selenium and python?

Tags:

I have one button from one LinkedIn page with this code:

<div class="primary-action-button"><a class="primary-action label" href="/requestList?displayProposal=&amp;destID=39959446&amp;creationType=DC&amp;authToken=Yr4_&amp;authType=OUT_OF_NETWORK&amp;trk=vsrp_people_res_pri_act&amp;trkInfo=VSRPsearchId%3A2998448551382744275729%2CVSRPtargetId%3A39959446%2CVSRPcmpt%3Aprimary">Send InMail</a></div>

Is there any way to click on an element just by its href link? Thanks

like image 511
Findios Avatar asked Oct 25 '13 23:10

Findios


People also ask

How do you click a href tag in Python Selenium?

We can click a link/button with its href link in Selenium webdriver. This can be achieved by multiple ways. We can use find_element_by_link_text() and find_element_by_partial_link_text() methods to perform this task.

How do you click a button using Selenium in Python?

We can click a button with Selenium webdriver in Python using the click method. First, we have to identify the button to be clicked with the help of any locators like id, name, class, xpath, tagname or css. Then we have to apply the click method on it. A button in html code is represented by button tagname.

How do I click a href in Selenium?

We can click a link by href value with Selenium webdriver. To identify the link with the href attribute we can take the help of the locators xpath or css selector. We shall use the findElement method and pass By. xpath or By.


2 Answers

Using selenium you could use the following code:

driver.find_element_by_link_text("Send InMail").click()
like image 127
Ben Smith Avatar answered Sep 28 '22 10:09

Ben Smith


The above answer driver.findElement(By.linkText("Send InMail")).click(); is in Java. In python, use find_element_by_link_text:

driver.find_element_by_link_text('Send InMail').click()

or something like this is sometimes helpful

driver.find_element_by_partial_link_text('Send').click()
like image 25
cacti5 Avatar answered Sep 28 '22 11:09

cacti5