Forums

XPATH cannot be found

I am attempting to locate a text element using Selenium in Python. While this process works flawlessly in my VS Code environment, I'm encountering difficulties when attempting the same on PythonAnywhere. The element I am trying to scrape cannot be found in the PythonAnywhere environment. My code is like

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.ChromeOptions()
driver.add_argument("--no-sandbox")
driver.add_argument("--headless")
driver.add_argument("--disable-gpu")

browser = webdriver.Chrome(options=driver)

try:
    browser.get('https://g.diamondhead.info')
    wait = WebDriverWait(browser, 10)
    element = wait.until(
    EC.presence_of_element_located(
        (By.XPATH, '/html/body/div[1]/div/div/div/div[1]/div/div[1]/div[2]/div[1]/div[1]')
    )
)
     print(element.text)
 finally:
     browser.quit()

And the error code is like this

Traceback (most recent call last): File "/home/YudaiOtahara/mysite/flask_app.py", line 15, in <module> browser.get('https://g.diamondhead.info') File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 442, in get self.execute(Command.GET, {'url': url}) File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 430, in execute self.error_handler.check_response(response) File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: net::ERR_TUNNEL_CONNECTION_FAILED (Session info: headless chrome=90.0.4430.212) Stacktrace: 0 0x55b26226de89 <unknown>

I tried with a different website like "google.com," and the Selenium code executed successfully. This suggests that the issue might be related to the specific website I'm trying to scrape. However, I encountered the same problem on both Google Maps and Google Search pages, indicating that the problem isn't isolated to a single website.

Has anyone had a similar experience or can anyone help me troubleshoot this issue? Thank you in advance!

Free accounts have limited internet access. See https://www.pythonanywhere.com/whitelist/