-1

due to some scenarios, I need to access chrome://downloads/ by headless mode but I got the error that looks like this:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#useragent"}
  (Session info: headless chrome=91.0.4472.124)

After trying to search around and try much time, but failed I've added a lot of options for chrome as below:

ChromeOptions options = new ChromeOptions()

options.addArguments('--headless', '--start-maximized', '--window-size=1366,768', 'disable-blink-features=AutomationControlled', 
    'privileged', '--test-type', '--disable-gpu', '--no-first-run', '--no-default-browser-check', '--ignore-certificate-errors', 
    '--disable-extensions', '--proxy-bypass-list=*', '--disable-dev-shm-usage', '--no-sandbox', '--lang=en_US', '--allow-running-insecure-content')

options.addArguments('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36')

options.setExperimentalOption('useAutomationExtension', false)

options.setExperimentalOption('excludeSwitches', ['enable-automation'])

options.addArguments('--proxy-server=\'direct://\'')

System.setProperty('webdriver.chrome.driver', ((RunConfiguration.getProjectDir() + '\\Data Files\\builds\\chromedriver_') + 
    GlobalVariable.chromeDriverVersion) + '.exe')

WebDriver driver = new ChromeDriver(options)

driver.navigate().to('chrome://version/')

WebElement userAgent = driver.findElement(By.cssSelector('#useragent'))

String txtUserAgent = userAgent.getText()

println(txtUserAgent)


But no luck happened

So anyone could help to resolve this problem, thanks a lot! PS: It can work fine on normal mode.

Chuoi
  • 5
  • 5

1 Answers1

0

Almost everything inside Chrome://downloads/ is under Shadow-root. You cannot directly access them.

Moreover, you'd see there are lot of nested shadow elements in DOM.

I have found a very good example here here on how to access Shadow elements.

def expand_shadow_element(element):
  shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
  return shadow_root
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • yeah, I already got the element by shadow dom already, It works on normal mode, but headless mode doesn't work, for the better example, at the chrome://version/ when no shadow dom here, it can't work in headless mode also, thanks! – Chuoi Jul 21 '21 at 07:44
  • Can you show some code ? That code that you shared is basically a config of driver. – cruisepandey Jul 21 '21 at 07:46
  • Sorry, how can i post a long comment :( – Chuoi Jul 21 '21 at 08:08
  • looks like some issue with headless mode, I could not even print `print(driver.title)` whereas without headless mode, it works. – cruisepandey Jul 21 '21 at 08:28
  • more strange, I tried this `print(len(driver.find_elements(By.ID, "useragent")))` and it is printing '0' that explains nosuch element exception, but I am not sure when it is only not working with headless mode, ideally it should. could be a bug – cruisepandey Jul 21 '21 at 08:32
  • yes, yay! thank you. so I should wait for the next fixing? – Chuoi Jul 21 '21 at 09:01
  • Is headless really required for this use case ? – cruisepandey Jul 21 '21 at 09:22
  • ah, actually I'm trying to automate the "Browser", it needs a lot of tip&trick should be used, sometimes I need to get the element, value... in silent mode for e.g But the normal mode is OK for now, I will wait for the next fixing if any, anyway thank you so much! have a great day! – Chuoi Jul 22 '21 at 02:00