So I've started a new project to help out a medium bussiness about solar-pannels that I work of ... Basically I want to take data from a specific websites with selenium and see it on my GUI that another friend of mine dealing with it... My main problem is when I open the website with selenium using python, the pop up cookie "Accepting all cookies" has been shown up and because I'm new to selenium i don't know how to handle it I've searching around 2 days about this problem and nothing that I tried is working so I assume that I'm a special case xD...
Here's all you guys need to know to help me out:
► URL ◄
https://www.kostal-solar-portal.com/#/
► Pictures ◄
[Picture 1] = https://i.stack.imgur.com/ZR89s.png |
[Picture 2] = https://i.stack.imgur.com/Zirft.png |
► Code ◄
`driver = webdriver.Chrome(PATH)
driver.implicitly_wait(10)
kostal_url = "https://www.kostal-solar-portal.com/#/"
driver.get(kostal_url)
driver.find_element_by_xpath('//*[@id="usercentrics-root"]//div/div/div[1]')
cookies = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,)))
cookies.click()`
► Error ◄
Traceback (most recent call last):
File "c:/Users/Hp/Desktop/ΜΑΚΗΣ/App/open_websites.py", line 27, in <module>
driver.find_element_by_xpath('//*[@id="usercentrics-root"]//div/div/div[1]')
File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 520, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1246, in find_element
'value': value})['value']
File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="usercentrics-root"]//div/div/div[1]"}
CodePudding user response:
The element Accept All is within #shadow-root (open).
Solution
To click on Accept All you have to use shadowRoot.querySelector()
and you can use the following Locator Strategy:
Code Block:
driver.get("https://www.kostal-solar-portal.com/#/") time.sleep(5) element = driver.execute_script("""return document.querySelector('#usercentrics-root').shadowRoot.querySelector("button[data-testid='uc-accept-all-button']")""") element.click()
References
You can find a couple of relevant discussions in:
- Can't locate elments within shadow-root (open) using Python Selenium
- How to get past a cookie agreement page using Python and Selenium?
CodePudding user response:
The element you trying to access is inside the Shadow Root.
So you first need to get inside that shadow root and then get the "Accept cookies" element.
You should also improve your locators.
This should work:
driver = webdriver.Chrome(PATH)
driver.implicitly_wait(10)
kostal_url = "https://www.kostal-solar-portal.com/#/"
driver.get(kostal_url)
shadow_section = driver.execute_script('return arguments[0].shadowRoot', find_element_by_id("usercentrics-root"))
shadow_section.find_element_by_css('button[data-testid="uc-accept-all-button"]').click()