Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking a link using selenium using python

Here is the link I'm trying to click:

<a href="#" onclick="OpenAddKeywords();return false;" id="btnAddKeywords">Add Keywords</a>

I tried a few options(listed below) but they didn't work; any ideas?

  1. self.br.find_element_by_xpath("//*[@id='btnAddKeywords']").click()
  2. self.br.execute_script("OpenAddKeywords();return false;")

This is the error I've got for execute_script:

Message: u'Error Message => \'Can\'t find variable: OpenAddKeywords\'\n caused by Request =>

And this is the one that I've got for xpath:

Message: u'Error Message => \'Unable to find element with xpath \'//*[@id=\'btnAddKeywords\']\'\'\n caused by Request =>

like image 739
Mark Avatar asked Jul 05 '13 23:07

Mark


People also ask

How do I click on a specific link in Selenium?

New Selenium IDE A hyperlink on a page is identified with the anchor tag. To click a link, we can use the link text locator which matches the text enclosed within the anchor tag. We can also use the partial link text locator which matches the text enclosed within the anchor tag partially.

How do I access links in Selenium?

A linkText is used to identify the hyperlinks on a web page. It can be determined with the help of an anchor tag (<a>). In order to create the hyperlinks on a web page, you can use anchor tags followed by the linkText.

Can we use Selenium with Python?

Selenium is an open source automation testing tool that supports a number of scripting languages like Python, C#, Java, Perl, Ruby, JavaScript, etc. depending on the application to be tested, one can choose the script accordingly.


2 Answers

As I mentioned in my own question here, the problem would be solved by means of ActionChains class; a brief code is here:

el = driver.find_element_by_id("someid")
webdriver.ActionChains(driver).move_to_element(el).click(el).perform()

The main problem is that in some cases, specially when you have a some javascript codes in your page, the DOM would change and the element you've found before will be staled. 'ActionChains' would keep it alive to perform actions on.

like image 86
Zeinab Abbasimazar Avatar answered Sep 28 '22 00:09

Zeinab Abbasimazar


You can try to use xpath like below. It is working for me because i have used last project.

driver.find_element_by_xpath("xpath").click()

Please try it...

like image 25
Kv.senthilkumar Avatar answered Sep 27 '22 23:09

Kv.senthilkumar