0

First, i'm an absolute beginner about this, have been trying to locate an element on a web page (lg.telin.co.id/lgnew/cacti.php), this element has the exact name when i wrote the code which is "login_username". However, i got the message " Unable to locate element " until now. Also this web page does not have any id

i used find_element_by_name but nothing work so far

from selenium import webdriver

browser=webdriver.Firefox(executable_path="C:\\Program Files (x86)\\Python37-32\\BrowserDriver\\geckodriver.exe")
browser.get("https://lg.telin.co.id/lgnew/cacti.php")

usernameStr = '123456'

username = browser.find_element_by_xpath("/input[@name='login_username']")

username.send_keys(usernameStr)

if the code work, text will be filled with characters i assume

2 Answers 2

1

Try switch to iframe before handling form input fields:

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

browser.switch_to.frame('cacti')

username = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.NAME, 'login_username')))
username.send_keys(usernameStr)
Sign up to request clarification or add additional context in comments.

Comments

0

You were pretty close. As the the desired elements are within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use the following solution:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name='cacti']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='login_username']"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@name='cacti']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='login_username']"))).click()
      
  • 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
    
  • Here you can find a relevant discussion on Ways to deal with #document under iframe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.