1

When I run the code below, I get selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="i0"]/input"} (Session info: chrome=83.0.4103.97).

However, in my terminal I can access the required element:

>>> steam_pressure_field = browser.find_element_by_xpath('//*[@id="i0"]/input')
>>> steam_pressure_field.get_attribute('value')
'0'

As you can see in the comment section of the code, I have tried waiting for a delay of 3 seconds to see if that made a difference. However, after doing this several times, the page stopped loading when I used the delay (maybe some anti-bot feature?)

So I'm trying to understand what is going on here, and how I can successfully access the fields I require. Is there something I am missing please?

BTW, The div IDs don't seem to be dynamic.

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


user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'
options = webdriver.ChromeOptions()
options.add_argument(f'user-agent={user_agent}')
browser = webdriver.Chrome(options=options)

url = 'https://www.tlv.com/global/US/calculator/superheated-steam-table.html?advanced=off'

# delay = 3
# try:
    # wait = WebDriverWait(browser, delay)
    # wait.until(EC.presence_of_element_located((By.ID, "body")))
    # browser.get(url)
    # print("Page is ready")
# except TimeoutException:
    # print("Loading took too much time")

browser.get(url)
steam_pressure_field = browser.find_element_by_xpath('//*[@id="i0"]/input')
print(steam_pressure_field.get_attribute('value'))
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Robin Andrews
  • 3,514
  • 11
  • 43
  • 111

1 Answers1

0

To print the value 0 you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://www.tlv.com/global/US/calculator/superheated-steam-table.html?advanced=off')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.inputPanel>div.Controlpanel input.inputText"))).get_attribute("value"))
    
  • Using XPATH:

    driver.get('https://www.tlv.com/global/US/calculator/superheated-steam-table.html?advanced=off')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='InputControlLabel' and text()='Steam Pressure']//following-sibling::input[1]"))).get_attribute("value"))
    
  • Console Output:

    0
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352