This has worked fine for months, but throws errors now. It's a pretty simple code: open Chrome and pull an element out. It's now telling me that it's unable to locate the element.
I've inspected the element with the Chrome window open and verify the XPATH and it matches to what is in the code exactly. I've taken a screenshot of the page it's running on and it all looks fine.
What am I missing?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import webbrowser
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
site_lat = 34.29421
site_lon = -97.44667
def watershed_lookup(site_lat, site_lon):
op=webdriver.ChromeOptions()
#op.add_argument('--headless')
op.add_argument('window-size=1920x1080')
url_water = 'https://mywaterway.epa.gov/community/'+str(site_lon)+','+str(site_lat)+'/overview'
driver = webdriver.Chrome(ChromeDriverManager().install(), options = op)
driver.get(url_water)
time.sleep(3)
element = driver.find_element_by_xpath("//*[@id=\"root\"]/div/div[4]/div[2]/div[2]/div[1]/div[1]/div/p[2]")
start1 = 'WATERSHED: '
end1 = '('
end2 = ')'
s = element.text
watershed_name = s[s.find(start1)+len(start1):s.rfind(end1)]
watershed_id = s[s.find(end1)+len(end1):s.rfind(end2)]
print("Watershed Name: " + watershed_name)
print("Watershed ID: " + watershed_id)
driver.close()
watershed_lookup(site_lat, site_lon)
The Xpath that you are using is an absolute Xpath, try finding that element using a relative Xpath. I have seen absolute xpaths working using dev tools and failing during run time. Use explicit wait until that element is visible/interactable and then verify.