0

I am new to Selenium and I try to find what's wrong with my code. I think the problem is not with slow loading of page(it is very slow), but when the code is reaching print('I am wating') program is waiting 30 seconds and after that I am having the error selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:. When it reaches this moment, there is no loading of the page or sth like that, it is just printing this information and after 30 seconds it gives these error. Code of my program is below.

from selenium import webdriver
from selenium.webdriver.common.by import By
import os
import typing
import types


class BrowserOperator(webdriver.Chrome):
    def __init__(self, page_url, teardown: bool = False):
        self._teardown = teardown
        self._pageUrl = page_url
        options = webdriver.ChromeOptions()
        options.add_experimental_option('excludeSwitches', ['enable-logging'])
        super(BrowserOperator, self).__init__(options=options)
        self.implicitly_wait(30)
        self.maximize_window()

def __exit__(self, exc_type: typing.Optional[typing.Type[BaseException]], exc: typing.Optional[BaseException], traceback: typing.Optional[types.TracebackType]):
    print(self._teardown)
    if self._teardown:
        print(self._teardown)
        self.quit()

def load_first_page(self):
    self.get(self._pageUrl)

def check_address_correctness(self):
    if self.current_url != self._pageUrl:
        return self.load_first_page()

def accept_cookies(self):
    print('I am wating')
    cookie_button = self.find_element(
        By.XPATH, '/html/body/div[1]/div[2]/div[2]/div/div[3]/button[2]')
    print(cookie_button)
    # cookie_button.click()
Paul
  • 1
  • 3

2 Answers2

0

maybe the xpath is not correct, you can use class_name etc...

I know a method to loate the absxpath by using edge

in the edge webpage, rightclick and click the Inspect, locate the element and rightclick and click Copy Copy FULL XPATH

the document
https://www.selenium.dev/documentation/webdriver/elements/finders/#find-elements-from-element

elements = driver.find_elements(By.TAG_NAME, 'p')
fruits = driver.find_element(By.ID, "fruits")
plants = driver.find_elements(By.TAG_NAME, "a")
Jiu_Zou
  • 463
  • 1
  • 4
0

Presumably you are trying to invoke click on the accept button. Using absolute xpath makes your test execution flaky. Instead you need to use logical xpaths. As an example:

  • Using css_selector:

    button[attribute='attribute_value']
    
  • Using xpath:

    //button[@attribute='attribute_value']
    

PS: In the above examples, attribute and attribute_value are placeholders.


Solution

Ideally, to locate and click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[attribute='attribute_value']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@attribute='attribute_value']"))).click()
    
  • 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
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352