Selenium [python] not clicking an <li> button - python

So I have been trying to use selenium to click an <li> button on an HTML which is in a <ul> tag and looks something like this:
...
<ul id="ulVisualization">
<li class="active" id="liMap">Map</li>
<li id="liBar" class="">Bar</li>
<li id="liLine">Line</li>
</ul>
...
I have been using the following command to get to the element using XPATH:
WebDriverWait(driver, 50).until(EC.presence_of_element_located((By.XPATH,"//li[#id='liBar']"))).click()
Yet for some reason that I have not been able to identify, this command is not able to find/click that button and the command hits its time-out.
I have even tried:
driver.find_element_by_xpath('//li[#id="liBar"]').click()
But that too was of no avail, throwing an error message saying NoSuchElementException: Message:
I would appreciate any and all help and thank you very much in advance.
EDIT:
Additionally, I have noticed that when I click on the button manually the HTML modifies to:
...
<ul id="ulVisualization">
<li class="" id="liMap">Map</li>
<li id="liBar" class="Active">Bar</li>
<li id="liLine">Line</li>
</ul>
...
Revealing the data I am trying to acquire later in the HTML code, which was not available before I clicked the button manually.

Since you want to click on the Element, try element_to_be_clickable, instead of presence_of_element_located
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//li[#id='liBar']"))).click()
Make sure the locator you are using is Unique, that is 1/1. Link to Refer
And also check if the Elements are in an iframe or in an shadow-root or Trying to find Element in a newly opened browser tab.
If none of them work, apply some time.sleep() and check if it works.

Related

Iterating in dropdown-menu

i'm having some troubles with trying to get a list in python..
I'm using Selenium Web Driver, Chrome specifically, and i have the next "button" :
<button id="btn" class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">Nope</button>
<ul id="ulDropdownNivel2" class="dropdown-menu">
<li>
text1
</li>
<li>
text2
</li>
<li>
text3
</li>
</ul>
So.. i have tried to use Select.class of Selenium Wd but, it's a button, and the class can't be used there... tried using it on the <ul> but it can't use Select neither...
Can't use smh like:
dropdoun = Select(driver.find_element_by_id('ID'))
for elm in dropdoun{ print(elm.text())}
Tried to figure a way to iterate through items... but i didn't get anything
i figured a way to click by xPath but, it doesn't work to me, cause text1, text2, text3 and so changes the order everytime you open the web
Any idea?
EDIT:
What i need here, is to iterate trough each item, and select the one that match with "text1", "text2" or "text3...
I think you should click button for dropdown will be opened:
opendropdownButton = driver.find_element_by_id("btn").click()
//here should be wait if now works
dropdownElements = driver.find_elements_by_xpath("//ul [#id="ulDropdownNivel2"]//a")
for elm in dropdownElements{ print(elm.text())}

How I use python selenium for click Navigation Bar With Dropdown

I want to click on "Page 1" for show list of sub menu
python code I used
driver.find_element_by_xpath("nav/div/ul/li[2]/a").click()
website code:
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>Page 1-1</li>
<li>Page 1-2</li>
<li>Page 1-3</li>
</ul>
</li>
<li>Page 2</li>
<li>Page 3</li>
</ul>
Image of nav
As your requirement is "I want to click on "Page 1" for show list of sub menu".
Instead of using absolute xpath , you can use linkText.
parent_page1 = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.LINK_TEXT, "Page 1")))
parent_page1.click()
if this linkText does not work out for you, you can use a relative xpath :
parent_page1 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='dropdown-toggle' and contains(text(),'Page 1')]")))
parent_page1.click()
Note that this code will click on Page 1 , if and only if it is clickable.
If it is a hover action then you can go ahead with action chain.
Note : This is for parent Page1 . Not for child. and based on the xpath you have tried nav/div/ul/li[2]/a
Your x-path is not correct. any x-path starts with / or //. This may be the issue.
try with the x-path //nav/div/ul/li[2]/a
in code,
driver.find_element_by_xpath("//nav/div/ul/li[2]/a").click()
I guess, you are using w3school bootstrap navigation bar. If yes, the page is inside frame, so first you need to switch to the frame as given below.
driver.switch_to_frame("iframeResult")
driver.find_element_by_xpath("//nav/div/ul/li[2]/a").click()

Why did changing my xpath make my selenium click work consistently?

I am running a series of selenium tests with python. I have a navigation on the page I'm testing that has this structure:
<ul>
<li class="has-sub">
<a href="#">
<span> First nav </span>
</a>
<ul style="display:block">
<li>
<a href="#">
<span> First subnav </span>
</a>
</li>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
</li>
<li>...</li>
</ul>
Now I am clicking on the first subnav, that is the first span, but clicking on First nav to open up that list then first subnav. I implement a webdriverwait, to wait for the element to be visible and click on it via it's xpath,
//span[1]
I often got timeout exceptions waiting for the subnav span to be visible after clicking on the nav, which made me think something was wrong with clicking on the first nav to open up the list. So I changed the xpath of the first nav (//span[1]) to
//li[#class='has-sub']/descendant::span[text()='First subnav']
and I never get timeout exceptions when waiting for subnav span to be visible now. So seems like it's always clicking on the nav span every time to open it up and give me no timeout when trying to get to the subnav. Anyone have any idea why that is?
Here is my python code as well:
inside LoadCommPage class:
def click_element(self, by, locator):
try:
WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((by, locator)))
print "pressing element " + str(locator)
self.driver.find_element(by, locator).click()
except TimeoutException:
print "no clickable element in 10 sec"
print self.traceback.format_exc()
self.driver.close()
inside main test (load_comm_page is an instance of LoadCommPage, where click_clement is defined):
load_comm_page.click_element(*LoadCommPageLocators.sys_ops_tab)
And another class for the locators:
class LoadCommPageLocators(object):
firstnav_tab = (By.XPATH, "//li[#class='has-sub']/descendant::span[text()='First nav']")
Xpath indexes begin at one, not 0 so the Xpath
//span[1]
is looking for the first span element in the html. Whereas
//span[2]
will look for the second span.

Double click works in Firefox but not chrome (Python/Selenium)

I have an element that requires a double_click().perform() action. It works great in Firefox, but doesn't in Chrome. A single click displays a menu, but a double click takes you to a directory. Here is my python/selenium step:
(webdriver.ActionChains(self.browser)).double_click(self.browser.find_element_by_id('nav-link-shopall')).perform()
The error message returns that the text I'm wanting to verify exists does not exist. Which is true if the double_click().perform() doesn't do its job.
I'm using Python 2.7.8 and Selenium 2.45.0.
I'm using Amazon.com as an example because the behavior is the same as the proprietary code I am testing. So here is the HTML code I'm attempting to click:
<div class="nav-left">
<div id="nav-shop">
<a href="/gp/site-directory/ref=nav_shopall_btn" class="nav-a nav-a-2" data-nav-tabindex="15" id="nav-link-shopall" tabindex="1">
<span class="nav-line-1">Shop by</span>
<span class="nav-line-2">"Department"
<span class="nav-icon nav-arrow" style="visibility:visible;"></span>
</span>
</a>
</div>
</div>
Try this:
variable = self.browser.find_element_by_id('nav-link-shopall')
actions = ActionChains(driver)
actions.move_to_element(variable)
actions.double_click(variable)
actions.perform()
You should use move to element.

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>
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']")
With CSS selectors:
next = driver.find_element_by_css_selector('li.next>a')
CSS Selectors are cool and useful: http://www.w3schools.com/cssref/css_selectors.asp
XPATH, not so much - it should be used only as a last resort.

Categories

Resources