How can i click an button inside 2 divs in selenium python?

I’m trying to make selenium click Button1 but for some reasons, I get the following error:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: Button1

I believe the error is happening because it is inside a div/ul/li tag but I can’t either figure out how to do it, I’m stuck.


<div id="contentArea">
<div class="pageNavigation" id="pageNavigation">

            <a href="#">Button1</a>

        <li class="last">       

            <a href="#">Button2</a> 

Python Code:

from selenium import *

driver = webdriver.Firefox()
driver.set_window_size(1366, 768, driver.window_handles[0])

Edit: I found out that the html is generated through javascript. my bad.


You can try the following methods


Or try like you said, to locate the element inside div/ul/li


And if that doesn’t work for some reason, try with ActionChains

For that, you need to import the module

from selenium.webdriver.common.action_chains import ActionChains

Then use it to click on the element,
this has been a life savior for me in some situations

action = ActionChains(driver)
element = driver.find_element_by_xpath("//*[text()='Button1']")