0

HTMLI want to select a textbox using XPath or any other locator, but I am unable to do so. The code is working fine for one part of the page, while it is not working using any locator for the other half of the page. I am not sure if my code is wrong or if something else is the problem.

I have attached the HTML part.

Here is my code:

import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome()
driver.get('Website')
driver.implicitly_wait(50)
driver.find_element_by_xpath('//*[@id="j_username"]').send_keys("Username")
driver.find_element_by_xpath('//*[@id="j_password"]').send_keys("Password")
driver.find_element_by_xpath('//*[@id="b_submit"]').click()
driver.find_element_by_xpath('//*[@id="15301"]/div[1]/a/span').click()
driver.find_element_by_xpath('//*[@id="22261"]/a').click()
driver.find_element_by_xpath('//*[@id="22323"]/a').click()
driver.implicitly_wait(50)
driver.find_element_by_xpath('//*[@id="filterRow"]').clear()

The last line is where I am getting the following error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="filterRow"]"}
Kirti Pal
  • 3
  • 3
  • Try //input[@name="pattern"] – RichEdwards Jul 29 '20 at 07:24
  • That element in the screenshot doesn't have an id – RichEdwards Jul 29 '20 at 07:24
  • Can you please be more specific about one half and another half. There is no such terminology in web pages. Either it is same page or going to next / new page. Also as @RichEdwards mentioned, there is no id in HTML snippet shared by you. You can use name attribute if you are trying to access same element as in attached snippet. xparh = //input[@name='pattern'] – rahul rai Jul 29 '20 at 07:48
  • @RichEdwards Added new screenshot. And tried using //input[@name="pattern" , still getting same error. – Kirti Pal Jul 29 '20 at 12:50
  • @rahul rai: I have tool bar on one side of my webpage with multiple tabs and each tab opens the related element in the same webpage. I am able to click each tab in the tool bar using xpath but unable to access any of related element. – Kirti Pal Jul 29 '20 at 13:08
  • @Kirti - pretty much all the responses here should work. There's something else in play. Timeout errors are because you can't find the object within the wait. Do you know how to debug scripts? (insert a breakpoint and step through the execution) you need to 100% make sure the element you expext is present and run time – RichEdwards Jul 29 '20 at 13:40
  • Or can you share the URL so we can look first hand? - when you share the source, try using devtools from your chromedriver instance. pause the run before your failling line, hit f12 and make sure the object/table/element is present in that window – RichEdwards Jul 29 '20 at 13:41
  • @KirtiPal To address [NoSuchElementException](https://stackoverflow.com/questions/47993443/selenium-selenium-common-exceptions-nosuchelementexception-when-using-chrome/47995294#47995294) check [this](https://stackoverflow.com/questions/50315587/selenium-common-exceptions-nosuchelementexception-message-no-such-element-una/50315715#50315715) and [this](https://stackoverflow.com/questions/53441658/selenium-in-python-nosuchelementexception-message-no-such-element-unable-to/53442511#53442511) discussion. – undetected Selenium Jul 29 '20 at 13:51
  • @KirtiPal If you are still unable to address your issue, update the question with the text based HTML, I will give it a shot. – undetected Selenium Jul 29 '20 at 13:53

5 Answers5

0

Page may have not finished rendering when you try to find the element. So it will give NoSuchElementException

Try the following method

elem = driver.find_element_by_xpath('//*[@id="filterRow"]')
if len(elem) > 0
    elem[0].clear()

Hope this will help you

shayanmalinda
  • 1,925
  • 3
  • 12
  • 23
0

You can wait till the elements loads using wait -

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)

Filter_Row = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="filterRow"]')))
Filter_Row.clear()

Try the above code and see what happens.

Swaroop Humane
  • 1,770
  • 1
  • 7
  • 17
0

Try below solution

wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='filterRow']"))).clear()

Note: add below imports to your solution :

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
SeleniumUser
  • 4,065
  • 2
  • 7
  • 30
  • tried the code and it gives "selenium.common.exceptions.TimeoutException ". I tried increasing the wait time too, still getting same error – Kirti Pal Jul 29 '20 at 13:24
0

As in one of comments, you mentioned upon clicking tab a new page is opening. Can you please check if its opening in a new frame. Is so please switch to frame first where your element is using below statement:

driver.switch_to.frame(driver.find_element_by_name(name))

To navigate back to original frame you can use:

driver.switch_to.default_content()

rahul rai
  • 2,260
  • 1
  • 7
  • 17
0

using 'find_element_by_css_selector' driver.find_element_by_css_selector("input")

Yimin
  • 39
  • 3