I want to access a website and fetch information using Selenium. I have passed the web URL using a list (vro_list) and fetched information from each URL in a list (roe_list). Website links I'm accessing: https://www.valueresearchonline.com/stocks/44052/reliance-industries-ltd https://www.valueresearchonline.com/stocks/44811/tata-consultancy-services-ltd
Here is the code I have used -
def fetch_roe(link):
URL = link
browser.get(URL)
browser.maximize_window()
time.sleep(5)
roe = browser.find_element('xpath', '/html/body/section[2]/div/div/div[1]/div/div[4]/section[1]/div[1]/div/div[2]/div[2]/div/div/div[2]/table/tbody/tr[2]/td[2]/div')
return roe.text
roe_list = []
option = Options()
option.add_argument("start-maximized")
option.binary_location = brave_path
browser = webdriver.Chrome(executable_path=driver_path, options=option)
for url_link in vro_list:
print(url_link)
roe_item = fetch_roe(url_link)
roe_list.append(roe_item)
time.sleep(5)
browser.quit()
print(roe_list)
When I run this code, I receive an error saying -
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/section[2]/div/div/div1/div/div[4]/section1/div1/div/div[2]/div[2]/div/div/div[2]/table/tbody/tr[2]/td[2]/div"}
I checked the website opened using Selenium and found this -
It seems like the website is blocking the access of the Selenium web driver. You can also see 'Brave is being controlled by automated software.' This happened on the second iteration of the for loop. The first iteration ran fine and I got the desired result.
How can I bypass this to fetch the required information? Please help
I'm also sharing the error message received on the console of Jupyter -
NoSuchElementException Traceback (most recent call last)
Input In [29], in <cell line: 8>()
8 for url_link in vro_list:
9 print(url_link)
---> 10 roe_item = fetch_roe(url_link)
11 roe_list.append(roe_item)
12 time.sleep(5)
Input In [27], in fetch_roe(link)
6 time.sleep(5)
8 #name = browser.find_element('xpath', '/html/body/div[3]/h1/span')
----> 9 roe = browser.find_element('xpath', '/html/body/section[2]/div/div/div[1]/div/div[4]/section[1]/div[1]/div/div[2]/div[2]/div/div/div[2]/table/tbody/tr[2]/td[2]/div')
11 return roe.text
File ~\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py:855, in WebDriver.find_element(self, by, value)
852 by = By.CSS_SELECTOR
853 value = '[name="%s"]' % value
--> 855 return self.execute(Command.FIND_ELEMENT, {
856 'using': by,
857 'value': value})['value']
File ~\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py:428, in WebDriver.execute(self, driver_command, params)
426 response = self.command_executor.execute(driver_command, params)
427 if response:
--> 428 self.error_handler.check_response(response)
429 response['value'] = self._unwrap_value(
430 response.get('value', None))
431 return response
File ~\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py:243, in ErrorHandler.check_response(self, response)
241 alert_text = value['alert'].get('text')
242 raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here
--> 243 raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/section[2]/div/div/div[1]/div/div[4]/section[1]/div[1]/div/div[2]/div[2]/div/div/div[2]/table/tbody/tr[2]/td[2]/div"}
(Session info: chrome=105.0.5195.102)
Stacktrace:
Backtrace:
Ordinal0 [0x00A2C0A3+2212003]
Ordinal0 [0x009C2CC1+1780929]
Ordinal0 [0x008D465D+804445]
Ordinal0 [0x00903475+996469]
Ordinal0 [0x0090363B+996923]
Ordinal0 [0x00931382+1184642]
Ordinal0 [0x0091EC64+1109092]
Ordinal0 [0x0092F5B2+1177010]
Ordinal0 [0x0091EA36+1108534]
Ordinal0 [0x008F83C9+951241]
Ordinal0 [0x008F9396+955286]
GetHandleVerifier [0x00CD9CE2+2746722]
GetHandleVerifier [0x00CCA234+2682548]
GetHandleVerifier [0x00ABB34A+524234]
GetHandleVerifier [0x00AB9B60+518112]
Ordinal0 [0x009C9FBC+1810364]
Ordinal0 [0x009CEA28+1829416]
Ordinal0 [0x009CEB15+1829653]
Ordinal0 [0x009D8744+1869636]
BaseThreadInitThunk [0x76A4FA29+25]
RtlGetAppContainerNamedObjectPath [0x77C07A9E+286]
RtlGetAppContainerNamedObjectPath [0x77C07A6E+238]