1

I'm trying to automate some stuff using Selenium but I'm having some trouble locating a particular element. I've located literally hundreds of other elements on different pages using the same method but for some reason this element just doesn't want to be located. I think maybe it's due to the HTML on the page rather than the python script? Although I can't change the HTML so if that is the case I'm going to need to figure out a workaround somehow.

I've got the test HTML in a file in the same directory as the python called "index.html"

Here's the code for it:

<body>
<tbody>
<tr><td class="rd-xc-Kd-kl-lf"><table style="position: relative; left: 0px;" id=":a" class="rd-xc-Kd-kl" cellpadding="0" cellspacing="0"><tbody><tr><td><div class="rd-xc-Kd-Bj-Gm">Drag a photo here</div><div class="rd-xc-Kd-Bj-Fm">Or, if you prefer...</div><div id=":d"><div style="-moz-user-select: none;" role="button" class="a-b-c d-u d-u-F">Select a photo from your computer</div></div>   </td></tr></tbody></table><div id=":c" style="display:none" class="d-Zb rd-jl-Ub-Yd"><div class="rd-jl-Li">Uploading...</div><div aria-valuenow="0" aria-live="polite" role="progressbar" class="a-b-c Ub-Vb-Wb rd-d-Yb-Zb"><div style="width: 0%;" class="Ub-Vb-O"></div></div></div></td></tr>
</tbody>

</body>

(That's just some code taken from the YouTube/Google+ drag and drop image upload form. I'm trying to automate updating my cover photo every week)

and here's my test python code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import os


driver = webdriver.Firefox()
driver.implicitly_wait(20)

driver.get(os.getcwd()+"\index.html")

element = driver.find_element_by_xpath("//td[@class='rd-xc-Kd-kl-lf']")
print element
print element.text

The error I get is the NoSuchElementException like this:

NoSuchElementException: Message: u'Unable to locate element:{"method":"xpath","selector":"//td[@class=\'rd-xc-Kd-...\']"}' ; Stacktrace: at FirefoxDriver.prototype.findElementInternal...

I'm not really sure where to start solving this problem. I'm tried using other selectors, such as find_element_by_id for the id "id=':a'" but that didn't work either.

Any help is appreciated.

2
  • 1
    The <table> tags are missing on the original html or did you remove them? Commented Sep 26, 2013 at 8:55
  • The class of your target td is perhaps dynamically generated. You can try selecting with a text content condition, such as //td[starts-with(table/tbody/tr[1]/td, "Drag a photo here")], i.e. select td cells that contain a table, in which the first tr row contains a cell starting with text "Drag a photo here". (Without the complete HTML source it's difficult to give you a correct answer) Commented Sep 26, 2013 at 9:03

1 Answer 1

2

Have you tried using the xpath checker in firefox at all? That has definitely helped me in the past. https://addons.mozilla.org/en-US/firefox/addon/xpath-checker/?id=1095

Also, you might try:

driver.find_element_by_class_name('rd-xc-Kd-Bj-Gm')
Sign up to request clarification or add additional context in comments.

1 Comment

I checked the xpath using another add-on in firefox called "firebug" it gave me a different xpath than what I was getting using inspect element from Chrome. It solved my problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.