Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I used firebug in Firefox to get a xpath for a link that did not have an ID assigned to it. The link is a javascript link with an image as the actual button. I'd like to be able to click this link but it's not working.

The actual xpath is '/html/body/div[2]/div/div/div[3]/div/div/table/tbody/tr[1]/td[2]/form/table/tbody/tr[1]/td/div[1]/div/table/thead/tr[2]/th[1]/a/img'

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()

URL = 'http://example.com'

driver.set_window_size(1024, 768)
driver.get(url)

link = driver.find_element_by_xpath(//*[@id="//html/body/div[2]/div/div/div[3]/div/div/‌​table/tbody/tr[1]/td[2]/form/table/tbody/tr[1]/td/div[1]/div/table/thead/tr[2]/th‌​[1]/a/img"])
for link in links:
 link.click()

I get the following error: is either invalid or does not result in a WebElement. The following error occurred:\nInvalidSelectorError: Unable to locate an element with the xpath expression //*[@id=/html/body/div[2]/div/div/div[3]/div/div/table/tbody/tr[1]/td[2]/form/table/tbody/tr[1]/td/div[1]/div/table/thead/tr[2]/th[1]/a/img because of the following error:\n[Exception... "The expression is not a legal expression." code: "12" nsresult: "0x805b0033 (SyntaxError)" location: "<unknown>"]' ; Stacktrace:

<a onclick="if(typeof functionx == 'function'){functionx(document.getElement…,'liststuff','');}return false" href="#">

   <img style="border: 0px;" onmouseover="this.src='/add-hover.gif';" onmouseout="this.src='/add.gif';" alt="Add" src="/icon-add.gif"></img>

</a>
share|improve this question
    
Could you show the relevant html code that contains the link, or provide a link to the web-site? Thanks. –  alecxe Jul 23 '14 at 20:56
    
@alecxe sorry I can't provide the url because it's on an internal server to a company. –  user2242044 Jul 23 '14 at 20:59
    
A guess: the <a> tag may have an img attribute instead of a nested <img> tag. If that's true, try changing a/img to a/@img at the end of your XPath. –  unutbu Jul 23 '14 at 21:03
    
@user2242044 the xpath you have is very fragile. Provide an html code so that we can see the link there. –  alecxe Jul 23 '14 at 21:04
    
@alecxe I added the element in the original post, but I can't add in the full html code –  user2242044 Jul 23 '14 at 21:11

1 Answer 1

up vote 3 down vote accepted

Instead of having an absolute path to the element, rely on it's parent and attributes:

link = driver.find_element_by_xpath('//a[@onclick and @href = "#" and img/@alt = "Add"]')
share|improve this answer
    
this executes will now code issues, but does not open the page that gets opened when I click the link manually. –  user2242044 Jul 23 '14 at 21:23
    
@user2242044 I've updated the xpath expression, please check. –  alecxe Jul 23 '14 at 21:24
    
still same result. No errors but not opened link. PyCharm gives the following result Process finished with exit code 0 –  user2242044 Jul 23 '14 at 21:26
    
This worked! I accidentally removed the link.click() line. Thank you so much. Marking as best answer. –  user2242044 Jul 23 '14 at 21:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.