2

When I access the chrome://settings/help site and tried to access the latest version of Google through Xpath or other channels, I couldn't access it.

I just want to get the string of the latest version of Chrome.

Are there any reasons or specific cases for not recognizing xpath?

Error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="main"]//settings-about-page//settings-section[1]/div[2]/div[2]/div[2]"} (Session info: chrome=114.0.5735.199);

please. help.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import time

service = Service(executable_path="chromedriver")
options = webdriver.ChromeOptions()
options.add_argument('start-maximized')
driver = webdriver.Chrome(service=service, options=options)

driver.get('chrome://settings/help')
time.sleep(5)
update_check = driver.find_element(By. XPATH, '//*[@id="main"]//settings-about-page//settings-section[1]/div[2]/div[2]/div[2]').text
print(update_check)
Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
Xelti
  • 23
  • 3
  • can you add the full Traceback of the error you're getting? – Ajeet Verma Jul 12 '23 at 06:21
  • @AjeetVerma selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="main"]//settings-about-page//settings-section[1]/div[2]/div[2]/div[2]"} (Session info: chrome=114.0.5735.199); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception – Xelti Jul 12 '23 at 06:25
  • the error clearly tells that the given XPATH is not correct/available on the page – Ajeet Verma Jul 12 '23 at 06:27
  • Please always provide a minimum reproducible work/code snippets in order to reproduce the error and thus provide you some way around to solve it. https://stackoverflow.com/help/minimal-reproducible-example – Ajeet Verma Jul 12 '23 at 06:28
  • @AjeetVerma Yeah.. but I don't know why.. Is there any other way to access that path? – Xelti Jul 12 '23 at 06:29
  • I could tell if you can provide the URL or the HTML of the Page – Ajeet Verma Jul 12 '23 at 06:33
  • @AjeetVerma put this in Chrome: `chrome://settings/help` this really does not seem to be working, I tried a couple of combinations. I do wonder if Google changed something specifically for this `chrome://settings/help` page... – BernardV Jul 12 '23 at 06:34
  • @AjeetVerma I just edited my code.. – Xelti Jul 12 '23 at 06:35
  • @AjeetVerma I changed my code url 'chrome://settings/help' but same problem is occured.. – Xelti Jul 12 '23 at 06:40
  • the reason you're unable to locate the element by XPATH is because of many `shadow-room` which can not be found without switching to them – Ajeet Verma Jul 12 '23 at 07:50

1 Answers1

0

Given your XPATH, I suppose you're trying to get the text of your installed Chrome version on your machine.

Since the page is highly embedded with many shadow-root elements that make it impossible to locate the elements that are embedded inside the shadow-root using the usual locator strategies such as XAPTH, CSS Selector, ID, etc.

Here's how you can do it:

import time
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('start-maximized')
driver = webdriver.Chrome(options=options)

driver.get('chrome://settings/help')

time.sleep(2)
update_check = driver.execute_script("return document.querySelector('settings-ui').shadowRoot.querySelector('settings-main').shadowRoot.querySelector('settings-about-page').shadowRoot.querySelectorAll('settings-section')[0].querySelector('div.secondary').getInnerHTML();")
print(update_check)

Output:

Version 114.0.5735.199 (Official Build)  (64-bit)

References:

  1. Selenium: how to get element in shadow root of html page code?
  2. How to click the button on Visa exchange rate calculator using Selenium?
  3. Scraping elements inside the #shadow-root web element
Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24