Eduardo M Eduardo M - 1 month ago 5
Python Question

Impossible to locate an element in selenium

I'm trying to find one of these elements (vote up or vote down):

<div class="votingWrapper ">
<span class="voteBtn voteUp " data-postid="c4fcff79-7f73-493c-b6d8-f05ff1962897"></span>
<span class="postRank">
<span class="rankWrapper wrapper_up" data-points="1320">+1.3k</span>
<span class="rankWrapper wrapper_down" data-points="512">-512</span>
</span>

<span class="voteBtn voteDown " data-postid="c4fcff79-7f73-493c-b6d8-f05ff1962897"></span>
</div>


I tried to locate it by class, id, xpath (a lot of types) but I got NoSuchElementException for all attempts.

element = driver.find_element_by_id("c4fcff79-7f73-493c-b6d8-f05ff1962897")


element = driver.find_element_by_class_name("voteBtn voteDown ")


element = driver.find_element_by_xpath('//*[@id="c4fcff79-7f73-493c-b6d8-f05ff1962897"]/div[1]/span[3]')


I searched for 3 hours how to do that but I failed. It's impossible.

Can anyone give me some tip?

Answer

try the following XPaths (in case they are not in a frame):

to find voteUp:

//span[contains(@class,"voteBtn voteUp")]

to find voteDown:

//span[contains(@class,"voteBtn voteDown")]

If they are inside a frame (check whether the elements you are trying to find, are child elements of an iframe tag), then first find the frame and then switch to it. then use the XPath to find elements.