Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python, locating and clicking a specific button with selenium

Using python and selenium I need to locate and click a specific button on a webpage. While under normal circumstances this can be done with the commands

next = driver.find_element_by_css_selector(".next")
next.click()

that won't work in this case due to the webpages coding. Where both the button leading to the previous page and the one leading to the next share the same class name. Thus this code will only go back and forth between the first and second page

As far as I can tell the only way to tell the two buttons apart, is that the button leading to the previous page lies within

<li class="previous">
    #button
</li>

and the one that leads to the next page lies within

<li class="next">
    #button
</li>

Is there some way of having selenium only select and click the button that lies in the "next" li class?

The complete button code:

the previous button:

<li class="previous">
    <a class="next" rel="nofollow" onclick="qc.pA('nrForm', 'f76', 'QClickEvent', '1', 'f28'); return false;" href="">
        Previous
    </a>
</li>

the next button:

<li class="next">
    <a class="next" rel="nofollow" onclick="qc.pA('nrForm', 'f76', 'QClickEvent', '3', 'f28'); return false;" href="">
        Next
    </a>
</li>
like image 752
user3053161 Avatar asked Dec 08 '13 18:12

user3053161


1 Answers

I think you should use .find_element_by_xpath(). For example:

next = driver.find_element_by_xpath("//li[@class='next']/a")
prev = driver.find_element_by_xpath("//li[@class='previous']/a")

or:

next = driver.find_element_by_xpath("//a[text()='Next']")
prev = driver.find_element_by_xpath("//a[text()='Previous']")
like image 90
mchfrnc Avatar answered Sep 25 '22 03:09

mchfrnc