no such element: Unable to locate element using chromedriver and Selenium in production environment

后端 未结 4 531
故里飘歌
故里飘歌 2021-01-04 13:29

I have a problem with selenium chromedriver which I cannot figure out what\'s causing it. Some weeks ago everything was working OK, and suddenly this error started to show u

相关标签:
4条回答
  • 2021-01-04 13:44

    Whenever I encounter strange issues in Selenium like this, I prefer retrying to find the particular element which is causing intermittent troubles. One way is to wrap it around a try-except block:

    try:
       sidebar = browser.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/a')
    except NoSuchElementException:
       time.sleep(10)
       print("Unable to find element in first time, trying it again")
       sidebar = browser.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/a')
    

    You could also put the try code in a loop with a suitable count variable to make the automation code work. (Check this). In my experience with JAVA, this idea has resolved multiple issues.

    0 讨论(0)
  • 2021-01-04 13:45

    It's report that the element not found error after you supplying the login , so I think the login failed and the page redirected to somewhere. You can use screenshot option to take a screenshot of the page and then see which page the driver load.

    driver.save_screenshot("path to save screen.jpeg")
    

    Also you can save the raw html code and inspect the same page.

    Webdriver Screenshot

    Using Selenium in Python to save a webpage on Firefox

    0 讨论(0)
  • 2021-01-04 13:49

    A couple of things as per the login_(browser) method:

    • As you have identified the Login button through:

      login = browser.find_element_by_xpath('/html/body/div[1]/div/button')
      

      I would suggest rather invoking send_keys("\n") take help of the onclick() event through login.click() to mock the clicking of Login button as follows:

      login = browser.find_element_by_xpath('/html/body/div[1]/div/button')
      login.click()
      
    • Next when you identify the sidebar induce WebDriverWait for the element to be clickable as follows:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="sidebar"]/ul/li[1]/a'))).click()
      
    • As you mentioned your code code block works perfect in macOS 10.11 environment but throws the following error in the production environment (Linux) it is highly possible that different browsers renders the HTML DOM differently in different OS architecture. So instead of absolute xpath you must use relative xpath as follows:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@attribute='value']"))).click()
      

    A couple of things as per the initiate_webdriver() method:

    • As per Getting Started with Headless Chrome the argument --disable-gpu is applicable only for Windows but not a valid configuration for Linux OS. So need o remove:

      option.add_argument('--disable-gpu')
      

    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
    
    0 讨论(0)
  • 2021-01-04 14:02

    You need to wait until the element is visible or else you will get this error. Try something like this:

    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support.expected_conditions import visibility_of_element_located
    from selenium.webdriver.common.by import By
    TIMEOUT = 5
    
    ...
    xpath = '//*[@id="sidebar"]/ul/li[1]/a'
    WebDriverWait(self.selenium, TIMEOUT).until(visibility_of_element_located((By.XPATH, xpath)))
    browser.find_element_by_xpath(xpath)
    ...
    
    0 讨论(0)
提交回复
热议问题