How can I check next iteration in for loop? - python

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")

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.

Python Selenium didn't find Element by ID

I am trying to find an input field by Id. The input field looks like
HTML:
<input id="hgz" onkeyup="runUpKey('form_hgzii);">
Python:
driver.find_element_by_id("hgz").send_keys("test")
Result:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="hgz"]"}
While iterating through all input fields
for inputs in driver.find_elements_by_tag_name('input'):
print(inputs.get_attribute("id"))
Python prints the correct ID, when im changing the code to
for inputs in driver.find_elements_by_tag_name('input'):
if inputs.get_attribute("id") == "hgz":
inputs.send_keys("test")
it also does not work.
Anyone an Idea?
Are you running this code right after opening the page? Maybe the DOM didn't finish loading. Try putting a sleep statement like sleep(5) before trying to get element by id

Cant click on button by css_selector in python with selenium

I have navigated to a (https://gmet.edupage.org/) webpage, logged in and got to the final step to complete my code, which is to click a next day arrow which cycles through the days of a calendar. Thing is while I have done this many times before the program still returns an error selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"next.fa.fa-angle-right"}
I wonder why cant it get the element when the source webpage code looks like this:
<span class="next fa fa-angle-right" data-smer="1" style="display:inline-block"></span>
My code looks like this:
next_page_button = driver.find_element_by_x('.next.fa.fa-angle-right')
next_page_button.click()
Any ideas on why this migh not work?
this should work:
next_page_button = driver.find_element_by_xpath("//span[#class='next fa fa-angle-right']")
next_page_button.click()
The issue was pretty simple I just had to add a time.sleep command so that the page has time to load.

find_element_by_css_selector('a').get_attribute('href') returns NoSuchElementException

I am trying to scrape the target website for product_links. The program should open the required URL in the browser and scrape all the links with a particular class name. But for some reason, I am getting a NoSuchElementException for this piece of code
links = driver.find_elements_by_class_name("styles__StyledTitleLink-mkgs8k-5")
for link in links:
self.driver.implicitly_wait(15)
product_links.append(link.find_element_by_css_selector('a').get_attribute('href'))
I tried printing out the text in each link with link.text in the for loop. The code is actually selecting the required elements. But for some reason is not able to extract the href URL from each link. I am not sure what I am doing wrong.
This is the entire error message
NoSuchElementException: Message: no such element: Unable to locate
element: {"method":"css selector","selector":"a"} (Session info:
chrome=83.0.4103.106)
Error seems there is no css element with 'a' so you need to try with other locators to identify elements. try with xpath=//a[contains(text(),'text of that element')]
You are looking for a class name generated by a builder, check the random string at the end of the class name, these classes won't be found in every web.
if you want to scrape them, find a different generic class or find all classes with a subset string "StyledTitleLink"
Here's how to do it with JQuery
You should try and find a different solution to your problem

Categories

Resources