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.
Related
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)
I am trying to scrape a JS website. I used Selenium and everything works well until I got this error. The website has a table and I am working on it.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[26]/td[4]"}
(Session info: chrome=84.0.4147.125)
In the XPath
25th and 27th iterations have 4. td
/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[26]/td[4]
but 26th doesn't have a 4th td.
I try to solve the problem with using "pass in loop" like this:
if(bool(driver.find_element_by_xpath("/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[26]/td[***+str(j)+***] ").is_enabled)):pass
But as soon as selenium sees the path it is giving an error without checking. I understand that "is_enable" method works for nothing.
The question is: how can I pass the iteration without Selenium interrupt?
try:
driver.find_element_by_xpath("./html/body/table/tbody/tr[2]/td["+str(j)+"]/table/tbody/tr["+str(i)+"]/td[4]")
except NoSuchElementException:
continue
If there is no element in the XPath, It returns null. It works. Thank you #JaSon
Have you tried something like this? (You can easily boot it up with for loop)
if driver.find_element_by_xpath("/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[26]/td[1(Can't boosted with for loop):
print("Found object")
else:
print("Can't find object")
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']")))
I am trying to use this XPath in Selenium with Python but it's generating an error: 'Message: no such element: Unable to locate element':
driver.find_element(By.XPATH, '//div[contains (#class, "files-list-grid-view")]/div[1][contains (#class, "folder")]').click()
This code works in Dev Tools with $x().
I need a marked element.
If the dataref node value is unique and it is not changing dynamically then you can use the below xpath :
element = driver.find_element_by_xpath("//div[#dataref='folder-0']")
If you are still getting an error then try to give some delay before locating it like below :
from time import sleep
sleep(3)
element = driver.find_element_by_xpath("//div[#dataref='folder-0']")
# And perform some action here
Still it doesn't work then check for frame or iframe using //iframe or //frame locators and if there are any matches and the locator that you are trying to locate is in some frame then switch to it using the below line and try to run your code again :
from time import sleep
# Switch to corresponding frame
driver.switch_to_frame("frame locator")
# Wait for sometime
sleep(3)
# Try to find an element
element = driver.find_element_by_xpath("//div[#dataref='folder-0']")
# And perform some action here
I hope it works...
I'm trying to wait for the innerHTML element to load. Here is the generic version of my code:
element = WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.XPATH, XPATH)))
element = element.get_attribute('innerHTML')
Element is a tr tag inside of a table. This code is inside of a loop that is supposed to run 25x per page over thousands of ajax pages. After a certain amount of runs, I continue to receive this error:
selenium.common.exceptions.StaleElementReferenceException: Message: {"errorMessage":"Element is no longer attached to the DOM"...
Every time, this error stems from the second line of provided code. This leads me to believe the element is loading, but the innerHTML is not loading quickly enough, and this elicits the given error message. I've tried many ways to get around this without success.
How can I make my code wait for the innerHTML to load after the element's presence has been confirmed?
Good that you are using python, you could write the wait condition like this as well.
innerHTML = WebDriverWait(driver, 5).until(lambda driver: driver.find_element_by_xpath(XPATH).get_attribute("innerHTML"))
or maybe like this
WebDriverWait(driver, 5).until(lambda driver: driver.find_element_by_xpath(XPATH).get_attribute("innerHTML") == "expected text")