Drop down list selenium python - python

Im trying to use a drop down list whit selenium :
browser.get('https://account.mail.ru/signup?from=main&rf=auth.mail.ru')
day = Select(browser.find_element_by_xpath('/html/body/div[1]/div[3]/div[3]/div/div/div/form/div[5]/div[2]/div/div[1]/div/div/select'))
dayDD=Select(day)
dayDD.select_by_index(3)
but i always get this error :
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div[3]/div[3]/div/div/div/form/div[5]/div[2]/div/div[1]/div/div/select"}
I dont know why it can't find xpath since i get it same way i get all my other xpaths

could you attach DOM page screen with list of elements?
At this stage I see 2 posibilities:
- Elements are not loaded
Use implicit or explicit wait : https://www.geeksforgeeks.org/waits-in-selenium-python/?ref=rp
- Xpath is not correct
Maybe learn how to use relative path : https://www.tutorialspoint.com/xpath/xpath_relative_path.htm

Related

How to click on a webpage element with Python

Imgur Web Element
I want to write a line of code that clicks on the ellipsis on the webpage. I've attached the element description.
I can't share the webpage as it is password protected
Here's what I tried:
element = driver.find_element_by_css_selector('.fas.fa-ellipsis-v')
element.click()
I am expecting the ellipsis to pop open but i get the following error:
Message: no such element: Unable to locate element: {"method":"css selector","selector":".fas.fa-ellipsis-v"}
First of all, please, do not use images when your want a code review or help, it is more easier to help with raw code.
In that case:
Message: no such element: Unable to locate element: {"method":"css selector","selector":".fas.fa-ellipsis-v"}
Try to use this code below instead of driver.find_element_by_css_selector([...]) :
WebDriverWait(your_driver, your_timeout).until(EC.visibility_of_element_located((By.CLASS_NAME, your_locator)))
or
WebDriverWait(your_driver, your_timeout).until(EC.visibility_of_element_located((By.CSS_SELECTOR, your_locator)))
After the WaitDriverWait, use your find and click code.
If you need more help locating elements by, try look the Selenium Docs.
It worked by using the following line:
driver.execute_script("arguments[0].click();", element)

Message: no such element: Unable to locate element python selenium

i have this error in selenium python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[24]/div[1]/div/div/div[3]/div[1]/div/button[1]"}
i want the element with this xpath:
driver.find_element_by_xpath("/html/body/div[24]/div[1]/div/div/div[3]/div[1]/div/button[1]").click()
if doesn't exist, then click other element with other xpath like:
driver.find_element_by_xpath("/html/body/div[24]/div[1]/div/div/div[3]/div[1]/div/button[2]").click()
everything is clear if element 1 doesn't exists to click, click to element 2
how can I do this in selenium python
thanks
NoSuchElementException generally occurs because of one the below reasons
Element locator is incorrect
Element is not rendered
Page is not rendered or loaded
Element is inside a frame
You can have a look, do a debug and based on that write your code. :)
This is not the exact solution, but this is the idea
if elem1.is_displayed():
elem1.click()
else:
elem2.click()
I'd suggest you using time.sleep(10) and a nested try block to catch your errors while it accomplishes what you want to do. For example:
time.sleep(10)
try:
driver.find_element_by_xpath("/html/body/div[24]/div[1]/div/div/div[3]/div[1]/div/button[1]").click()
except:
print("button1 could not be found... Now trying to retreive button2")
try:
driver.find_element_by_xpath("/html/body/div[24]/div[1]/div/div/div[3]/div[1]/div/button[2]").click()
except:
print("button1 nor button2 were not found...")
Note that the time.sleep(10) delays your code from executing anything after this line for 10 seconds, and this allows the page to load properly for elements to be located easier.

Selenium Python can't find element [duplicate]

This question already has answers here:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:
(9 answers)
Closed 1 year ago.
Hey guys im completely lost. I've tried everything find_by_xpath/by_class etc but it just won't work. I always get the can't find element error: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".ms-Button _3vq4lJQg9EQ9QnW_2iLCJo ms-Button--action ms-Button--command root-205"}
(Session info: chrome=91.0.4472.77)
next_but=web.find_element_by_id('idSIButton9')
time.sleep(1)
next_but.click()
pwin=web.find_element_by_id("i0118")
pwin.send_keys(PW)
time.sleep(2)
next_but2=web.find_element_by_id("idSIButton9")
next_but2.click()
web.get(link)
rules=web.find_element_by_class_name('ms-Button _3vq4lJQg9EQ9QnW_2iLCJo ms-Button--action ms-Button--command root-205')
I've also tried that the WebDriver waits for the element to load and it seems that the element isn't in another frame.Thats the button I'd like to be pressed
Please help me im completely lost thanks in advance
I believe this should be resolve with WebDriverWait :
Code to try it out :
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Regeln']/ancestor::button"))).click()
That's some ugly HTML. Try this:
driver.find_element_by_xpath("(//div[#data-focuszone-id='FocusZone197']/button)[4]")
Most of the HTML here looks dynamic which makes it hard to build bulletproof xpaths.
I would use the following as the aria-label should mostly stay static over time:
driver.find_element_by_xpath("(//div[#aria-label='Unterregisterkarte der Kategorie "E-Mail"']//button)[4]")
Looks like the class name you are using is dynamic. Try some thing like this
driver.find_element_by_xpath(".//button[text()='button text here']").click()
Update:
driver.find_element_by_xpath(".//span[text()='Regeln']").click()

Unable to find Input value using Xpath

New to python here. I am trying to have my script click a checkbox with a given input value. Unfortunately there is no input ID. Using find_element returns with error:
Unable to locate element
Using find_elements() returns a list and therefore I get the error:
'list' object has no attribute 'click'
This is the code I have had trouble with.
OldJob = driver.find_elements_by_xpath("//input[#value='0 ']")
OldJob.click()
Here is the html:
Any thoughts on how I can get around this?
To locate the <input> element you can use either of the following Locator Strategies as follows:
cssSelector:
driver.find_element_by_css_selector("input[name='RowNum'][value^='0'][type='checkbox']")
xpath:
driver.find_element_by_xpath("//input[#name='RowNum' and contains(#value, '0')][#type='checkbox']")

How do I click a button using Selenium in Python?

I'm trying to crawl review data on an app from a Google Play website page using Python and Selenium. What I'm trying to do here is to click the "Full Review" button to see the entire text of each review.
But I keep running into these errors each time:
ElementNotInteractableException: Message: element not interactable
or
AttributeError: 'list' object has no attribute 'click'
or
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".LkLjZd ScJHi OzU4dc "}
The element for the button is this:
<button class="LkLjZd ScJHi OzU4dc " jsaction="click:TiglPc" jsname="gxjVle">Full Review</button>
The xpath for the button is this:
//*[#id="fcxH9b"]/div[4]/c-wiz/div/div[2]/div/div[1]/div/div/div[1]/div[2]/div/div[26]/div/div[2]/div[2]/span[1]/div/button
And this is the code I'm using for xpath:
full_review = driver.find_elements_by_xpath('//*[#id="fcxH9b"]/div[4]/c-wiz/div/div[2]/div/div[1]/div/div/div[1]/div[2]/div/div[26]/div/div[2]/div[2]/span[1]/div/button')
full_review.click()
I can't find the button by class name, xpath or anything.
Could anyone help me figure out this problem? In addition, is it possible to find the element by jsname?
I would really appreciate if you could help.
Avoid using xpath whenever possible, it's the most brittle selector.
id > CSS > Xpath
For your button, this css selector should work:
button[jsname='gxjVle']
You'll probably need to specify the child as that probably won't be unique
Your XPath selector is a little bit flaky, you can simplify it to be something like:
//button[text()='Full Review']
You're using the wrong function, if you're looking for a single unique element you should be using WebDriver.find_element_by_xpath one, mind that element, not elements
It's better to use Explicit Wait just in case element is not immediately available in DOM (for example if you're testing application built using AJAX technology)
Assuming all above:
full_review = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Full Review']")))

Categories

Resources