1

I am trying to create a scraper, and for accessing the page I need to click on the Accept Cookies button. The HTML referring to the button is:

<div class="qc-cmp2-summary-buttons">
    <button mode="secondary" size="large" class=" css-1hy2vtq">
        <span>MORE OPTIONS</span>
    </button><button mode="primary" size="large" class=" css-47sehv">
        <span>AGREE</span></button></div>

The button I want to click is the second one, named "AGREE".

I tried:

driver.find_element(By.CLASS_NAME, " css-47sehv").click()

and I get error selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified

1
  • If the button is on a popup window, then you first need to find the window, often an iframe, then move focus to it driver.switch_to.frame(frame), and then look for the button and click on it. Commented Dec 4, 2022 at 18:53

2 Answers 2

1

The error you received caused by a space before the class name value.
Spaces between class names are used to separate between multiple class names.
So, to use this specific class name you could try this (without the space):

driver.find_element(By.CLASS_NAME, "css-47sehv").click()

But css-47sehv seems to be a dynamically generated class name so I'd advice to use more fixed attribute for locating that element.
Try this locator instead:

driver.find_element(By.XPATH, "//button[contains(.,'AGREE')]").click()

To give you better answer we need to see that web page and all your Selenium code

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! Indeed the white space was causing the issue
0

Try to use By.CSS_SELECTOR. Find element and copy its selector

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.