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.
<table>tags are missing on the original html or did you remove them?tdis 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. selecttdcells that contain atable, in which the firsttrrow contains a cell starting with text "Drag a photo here". (Without the complete HTML source it's difficult to give you a correct answer)