2

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]"}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Thomas
  • 37
  • 6

2 Answers2

4

The element Accept All is within #shadow-root (open).

kostal


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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you saved my day literally!... By the way that #shadow-root(open) link that you put there is written in Java so I just went straight up to your code block and copy all of it xD... All though I'm having a question seeing the below answer if i leave the driver.implicity_wait(10) should I'll be able to remove the sleep method? – Thomas Dec 14 '21 at 14:49
  • @Thomas Checkout the other embedded links and references as well. I'm not sure, but `implicity_wait()` may not be effective here, but still you may like to test it out. – undetected Selenium Dec 14 '21 at 15:06
1

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()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Thank you for your time... But I found more simple the above answer! :* – Thomas Dec 14 '21 at 14:52
  • His answer is not simpler, it just combines 2 actions in 1 complex JavaScript script... – Prophet Dec 14 '21 at 15:00
  • @DebanjanB I'm not sure. At least sometimes it makes code less clear and harder for debugging. In this specific case I don't know what approach is better. – Prophet Dec 14 '21 at 15:43