Home > Mobile >  selenium-driver :looking a solution for locating and clicking a button when i open google page
selenium-driver :looking a solution for locating and clicking a button when i open google page

Time:09-09

python selenium driver:

No thanks

when i open a google page the, there is a small window asking if i want to sign in or not. i want to click on "No thanks" button, which is as shown above.

i have tried these methods so far, but i keep getting errors. None of the following is working.

    #self.driver.find_element(By.CSS_SELECTOR, 'button.M6CB1c')
    #button=self.driver.find_elements(By.XPATH, '//button')
    #abc=self.driver.find_elements(By.NAME, 'ZUkOIc').click()
    #self.driver.find_element(By.TAG_NAME, 'button').click()

error message for the 1st line of code: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".M6CB1c.rr4y5c"}

CodePudding user response:

You should try this : driver.find_element(By.XPATH, '//button[@id="W0wltc"])

CodePudding user response:

selenium.common.exceptions.NoSuchElement is caused when the element is not in the page at the current time.

TLDR; What you're looking for is Explicit Wait in selenium. You need to use WebDriverWait with expected condition element_to_be_clickable.

When we load a page, modern pages tend to load javascript that can often manipulate DOM (html page objects). The proper way to handle this is to wait for the page or the required element to load, and then try to locate it.

The selenium waits section explains this very well with an example.

  • Related