1
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("chrome://settings")

advanced = driver.find_element_by_css_selector("#advancedToggle > span")
advanced.click()

The above code doesn't work. To see where the advanced variable was referring to, you can go to paste "chrome://settings" in your browser (only works if you're using chrome) and scroll down to the bottom where you see the "Advanced" toggle.

That toggle even has an id "advancedToggle" but I can't seem to find anything on this page by any method (id, class, css, xpath).

Is it possible that some pages are just automation-resistant (if that's a proper term)? If so, is there any way to tell which pages can't be automated on?

I've placed this last question in a block quote because this question was marked as a duplicate, but while the example has appeared somewhere else, I don't think there has been an answer for the question in the block...

IceTea
  • 598
  • 1
  • 6
  • 19

1 Answers1

2

You have to use /deep/ to pass through shadow-root element:

body /deep/ #advancedToggle > span

The code would be like this:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("chrome://settings")

advanced = driver.find_element_by_css_selector("body /deep/ #advancedToggle > span")
advanced.click()

Shadow-root element looks like this:

img

you can find it by pressing F12 and then switch to Elements tab. Then in the DOM you will find shadow-root.

Andrei Suvorkov
  • 5,559
  • 5
  • 22
  • 48
  • thanks! but how do I know whether a page has the `shadow-root` element? I don't seem to see `shadow-root` in the page source. Sorry if this question seems rather elementary! – IceTea Jul 27 '18 at 10:48
  • I have added a screenshot and explanation. Note: not every page has shadow-root elements. – Andrei Suvorkov Jul 27 '18 at 10:55
  • 1
    I couldn't find it using the Ctrl+F tool so thanks for the screenshot! – IceTea Jul 27 '18 at 11:26