Need help in selecting an option from a drop that opens up on hover, here is the html i'm currently looking at or you can just look into the attached image -
<ul id="yui_3_7_0_4_1390312781452_393" class="dropdown-menu">
<li id="yui_3_7_0_4_1390312781452_392" class="action" value="4001">
Promising
</li>
<li class="action" value="4003">
In Discussion
</li>
<li class="action" value="4004">
Phone screen
</li>
<li class="action" value="4005">
Interview
</li>
<li class="action" value="4006">
Offered
</li>
<li class="action" value="4007">
Hired
</li>
And here is the code i'm trying -
strings = driver.find_elements_by_class_name("action").text
for text in strings:
if text=='Offered':
text.click()
Using a CSS Selector, you could click it by doing:
driver.find_element_by_css_selector("ul.dropdown-menu[id^='yui'] li[value='4006']").click()
In order to achieve drop down menu selection mentioned in the question, you'll first have to mouse hover to drop down menu then hover mouse over element to select and the click on the element. Following is Java code, but the logic will help you to implement it in Python:
WebElement dropDownMenu = driver.findElement(By.id("yui_3_7_0_4_1390312781452_393"));
WebElement elementToSelect = driver.findElement(By.xpath(".//li[contains(text(), 'Offered')"));
Actions action = new Actions(driver);
action.moveToElement(dropDownMenu).moveToElement(elementToSelect).click().build().perform();
try this (ref:http://allselenium.info/mouse-over-actions-using-python-selenium-webdriver/)
from selenium.webdriver.common.action_chains import ActionChains
action = ActionChains(driver)
firstLevelMenu = driver.find_element_by_id("yui_3_7_0_4_1390312781452_393") #id of menu, or xpath of menu, whatever
action.move_to_element(firstLevelMenu).perform()
secondLevelMenu = driver.find_element_by_xpath("XPATH_OF_OFFERED")
action.move_to_element(secondLevelMenu).perform()
secondLevelMenu.click()
Related
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.
I am wondering how to scroll on a scrollable element in a web page. I found plenty of answers about how to scroll the whole page (working well) but here it's an inner specific div that is scrollable that I want to scroll to the bottom.
How can I do that with selenium for python?
I ran into a similar issue and solved it like this:
If this is your HTML:
<ul class="list">
<li class="list__element">1</li>
<li class="list__element">2</li>
<li class="list__element">3</li>
<li class="list__element">4</li>
<li class="list__element">5</li>
<li class="list__element">6</li>
<li class="list__element">7</li>
</ul>
Select all the child elements of your list like so:
list_elements = driver.find_elements(By.CLASS_NAME, 'list__element')
After that, you can loop over that list and scroll to a specific item.
Note: If your list progressively loads new list elements you need to accoaunt for that.
for comment in comments:
ActionChains(driver)\
.scroll_to_element(comment)\
.perform()
time.sleep(.1)
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())}
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()
I want to click on MAKE UP in the
left navigation, Please find attached image and link for the webpage
Image for the Webpage
Link for the Webpage
I am currently using the below code to click on the item but not
getting any result.I am able to acces the elements by class
name('has-sub').I can even print them but cant click them
obc = driver.find_elements_by_class_name('has-sub')
for ea in obc:
if ea.text == "Makeup":
ea.click()
Just for the more info below is the html code for the webpage
<li class="has-sub" style="height: 38px;">
Makeup
<ul class="submenu" style="top: 0px;">
<li>
<a id="SBN_facet_Face" href="http://shop.davidjones.com.au/djs/en/davidjones/beauty/face" escapexml="false">Face </a>
</li>
<li>
<a id="SBN_facet_Lips" href="http://shop.davidjones.com.au/djs/en/davidjones/beauty/lips" escapexml="false">Lips </a>
</li>
<li>
<a id="SBN_facet_Eyes" href="http://shop.davidjones.com.au/djs/en/davidjones/beauty/eyes" escapexml="false">Eyes </a>
</li>
<li>
<a id="SBN_facet_Nails" href="http://shop.davidjones.com.au/djs/en/davidjones/beauty/nails" escapexml="false">Nails </a>
</li>
<li>
<a id="SBN_facet_Brushes & Tools" href="http://shop.davidjones.com.au/djs/en/davidjones/beauty/beauty-brushes-accessories" escapexml="false">Brushes & Tools </a>
</li>
<li>
<a id="SBN_facet_Makeup" href="http://shop.davidjones.com.au/djs/en/davidjones/beauty/beauty-makeup" escapexml="false">All Makeup </a>
</li>
</ul>
</li>`enter code here`
Any help will be appreciated .
I am able to click using below code.
wait = WebDriverWait(driver, 10)
elements = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//li[#class='has-sub']")))
for element in elements:
if element.find_elements_by_link_text("Makeup"):
element.click()
break
innerElements = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//li[#class='has-sub open']/ul/li")))
for innerElement in innerElements:
if innerElement.text == "Face":
innerElement.click()
break
Hope this will help you.
Problem here is, you are trying to click on the element while the text is under the element. So what you are going to need to do is:
obc = driver.find_elements_by_xpath('//li[#class='has-sub']/a[contains(text(), 'Makeup')]')
I tested the xpath on your webpage and it worked.
As per the HTML you have provided, to click on MAKE UP in the left navigation pane, you can use the following code block :
obc = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[#class='aside all-open']/ul//li[#class='has-sub']/a")))
for ea in obc:
if 'Makeup' in ea.get_attribute("innerHTML"):
ea.click()
break