Selecting option from dropdown menu using Selenium; Unable to locate element - python

I need to select an element from a dropdown list. I'm using Selenium, but I'm having difficulty selecting the dropdown list itself to select from the available options and keep receiving an error "Message: no such element: Unable to locate element:". I've tried the following:
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
driver=webdriver.Chrome()
driver.get('mysite.com')
driver.implicitly_wait(10)
driver.find_element_by_xpath('//*[#id="xpath-example"]').click()
select.select_by_visable_text('option 1')

elem=driver.find_element_by_xpath('//*[#id="xpath-example"]')
sel = Select(elem)
sel.select_by_index(1)
sel.select_by_visible_text("option 1")
You need to find the elem and put it into the Select first of all. Then try one of the two options.

Can you try using by xpath click() action with:
//*[#id="x"]/option[1]
something like:
driver.find_element_by_xpath('//*[#id="x"]/option[1]').click()

A couple of things: first, you may want find_elements which returns a list as opposed to find element which returns an object.
Additionally, Because the element which you are trying to access may become visible at an unknown time, you could employ use of a timed wait, which (by your code) would always wait 10 seconds. Or you could make use of the webdriverwait function which would wait only until it was visible (which could be 2,3 or any time up to a timeout which you define)
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver=webdriver.Chrome()
driver.get('mysite.com')
try:
elem = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//*[#id="xpath-example"]')))
item = elem.select_by_visible_text('option 1')
except:
print("Couldn't find elem")
pass

Related

How to select an option from the non select dropdown list based on combobox using Python Selenium

Trying to automate one thing for my work, which was choosing one option from the dropdown list on the website below:
https://interparking-pl.my.site.com/abonament/s/?id=a0A58000000D7pZ
The Selenium automation didn't work in that case. After writing such code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_driver_path = r"C:/Users/.../Projects/chromedriver.exe"
service = Service(executable_path=chrome_driver_path)
driver = webdriver.Chrome(service=service)
driver.get("https://interparking-pl.my.site.com/abonament/s/?id=a0A58000000D7pZ")
wait = WebDriverWait(driver, 10)
abo_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="combobox-button-53"]')))
After executing I've got a message:
TimeoutException
In case of finding element by tag name or any other options, the following message pops up:
Message: no such element: Unable to locate element
The list is built on button tags and has a structure of lightning-basecombobox. It looks like there is no possibility to click on the dropdown list and choose the required option automatically.
Is it needed to do something different with such stuff?
What I expect is to use Selenium to choose between the options in the list.
The combobox is a <button> element.
Additionally the id i.e. combobox-button-53 is dynamically generated and is bound to chage sooner/later. They may change next time you access the application afresh or even while next application startup. So can't be used in locators.
Solution
To click element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.get("https://interparking-pl.my.site.com/abonament/s/?id=a0A58000000D7pZ")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[name='subscriptionsTypeId']"))).click()
Using XPATH:
driver.get("https://interparking-pl.my.site.com/abonament/s/?id=a0A58000000D7pZ")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#name='subscriptionsTypeId']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot;
When I load the page, I get the element ID (XPATH) as following. //*[#id="combobox-button-51"]
Maybe the number 51 isn't always the same? In that case, try:
//*[starts-with(#id,'combobox-button-5')]
Or just use //*[#name="subscriptionsTypeId"] , as another answer here allready mentioned.

locate an a element and click on it in selenium pyhton

I have this page and I want to click on the a element that sends an email(the one that is highlighted in the screenshot below
and I have tried to find this element using By.XPATH
email = driver.find_element(By.XPATH, "//a[contains(#class, 'email-old-32')]").click()
and By.CLASS_NAME
email = driver.find_element(By.CLASS_NAME, 'email-old-32').click()
and in both situations i'm getting an error no such element, does anyone know what am I doing wrong?
There are several possible thing that may cause this:
You need to wait for element to be loaded and to become clickable. WebDriverWait expected_conditions can be used for that.
If this is your case instead of email = driver.find_element(By.XPATH, "//a[contains(#class, 'email-old-32')]").click() try this:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#class, 'email-old-32')]"))).click()
The element class attribute value can be dynamic. Make sure it is not changing. In case it does - you need to find another, stable locator for that element.
The element can be inside iframe. In this case you will need to switch into the iframe.
Possibly you opened a new tab where the goal element is presented but forget to switch to a new tab.

How to access an element by xpath?

How to access an element on pic: 'captcha__human__submit-description' in a frame using xpath. I need to check the presence of this element, and depending on its presence, continue or terminate the program.
this combination does not find this element:
driver.switch_to(0)
driver.find_element_by_xpath("//dic[#class='captcha__human__submit-description']").text
xml data
try to switch it like this :
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[#onload='iframeOnload()']"))
driver.find_element_by_xpath("//div[#class='captcha__human__submit-description']").text
or with explicit waits :
wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[#onload='iframeOnload()']")))
some_text = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[#class='captcha__human__submit-description']"))).text
print(some_text)
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
As already given by CruisePandey, you can switch to frame and then access element using the XPaths. However, I believe you can also use CSS Selectors to do the same thing.
Using Selenium 3 -
driver.find_element_by_css_selector("div.captcha__human_submit-description")
or
driver.find_element_by_css_selector("div.captch__human__container > div:nth-child(2)")
Using Selenium 4 Beta version
driver.find_element(By.CSS_SELECTOR, "div.captcha__human_submit-description")
or
driver.find_element(By.CSS_SELECTOR,"div.captch__human__container > div:nth-child(2)")

locating hide element using selenium and python

I have an element that after clicking the button it builds a div with Ajax and I can't get the element.
Click button:
Show div:
But in my code doesn't work
vv = drive.find_element_by_xpath('//*[#id="vUPDATE_0001"]')
Error Message:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="vUPDATE_0001"]"}
I think what you're looking for is to wait until the div is visible. Luckily, selenium has just that!
You can read more over here.
By using WebDriverWait along with expected_conditions, your example code would look like this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
timeout = 10 # Wait 10 seconds. If it doesn't appear in 10 seconds then throw an error
vv = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.XPATH, '//*[#id="vUPDATE_0001"]')))
For many different circumstances, there are different conditions, such as wait for an element to be clickable, or wait an alert to pop up. We are using presence of element located, which is the same as find_element_by_[condition]. Then we are setting the condition with By.XPATH. There are many different conditions which represent their find_element_by_ alternative, such as By.ID, By.CLASS_NAME, and By.NAME.

How can I iterate through a drop-down list and click on each and every option? Getting StaleElementReferenceException error

I am using this code but I got clicked limited options in result after that it throws the error given below.
I want to click through all options, but I don't understand why it is happening. I have 50 options in my dropdown.
select = Select(wait(driver, 35).until(lambda driver: driver.find_element_by_id('BodyContent__company')))
options = select.options
for opt in options:
# for example
print(opt.text)
opt.click()
Error: StaleElementReferenceException: stale element reference:
element is not attached to the page document(Session info:
chrome=83.0.4103.116)
Any help would be appreciated.
With every click on the select element's options, the element (select) updates. That is why the StaleElementReferenceException error - the element has changed.
Try this. After identifying select, click all options using their index. After each click re-identify select to avoid Stale Element Reference Exception.
select = Select(WebDriverWait(driver, 35).until(EC.visibility_of_element_located((By.ID,'BodyContent__company'))))
for index in range(len(select.options)):
select.select_by_index(index)
select = Select(WebDriverWait(driver, 35).until(EC.visibility_of_element_located((By.ID,'BodyContent__company'))))
You will need these imports:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait, Select

Categories

Resources