简体   繁体   中英

I can't find an element in Selenium.(Python/Google)

This is the link https://webmail1.hostinger.com/

I want to locate the 2 input boxes and input my email and password, but i can't acess them. I can easily find them by inspecting the page but i can't find them by their xpath, selector, id etc.

I considered that they may be inside a frame and i acessed the frames within the page to no avail.

I also tried to insert a wait period in case there is something to load, but that didn't solve the problem. Here is how the related section of my code currenly stands:

driver.execute_script("window.open('https://webmail1.hostinger.com/');)
sleep(10)

iframes = driver.find_elements_by_tag_name("iframe")
driver.switch_to.frame(iframes[0])

element = WebDriverWait(driver, 30).until(
       EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div[1]/form/table/tbody/tr[1]/td[2]/input"))
    )

element.send_keys("test")

Thank you for your help

"execute_script" is a javascript method and its used to find an element that may not be accessible to the user, its used to find elements and not open the website. Ive also found the id of the input boxes.

use driver.get(URL) instead

from selenium import webdriver

driver = webdriver.Chrome("location of webdriver")
URL ='https://webmail1.hostinger.com/'

driver.get(URL)

driver.find_element_by_id('rcmloginuser').send_keys('your username')
driver.find_element_by_id('rcmloginpwd').send_keys('your password')

If you want to go to a URL and grab the id's and submit in python you should do something like this. Your tags were not in an iframe and we use webdriver waits instead of time.sleep()

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

driver = webdriver.Chrome("location of webdriver")
URL ='https://webmail1.hostinger.com/'

driver.get(URL)

WebDriverWait(driver, 30).until(
 EC.presence_of_element_located(By.ID('rcmloginuser'))).send_keys('your username')

WebDriverWait(driver, 30).until(
 EC.presence_of_element_located(By.ID('rcmloginpwd'))).send_keys('your pass')

WebDriverWait(driver, 30).until(
 EC.presence_of_element_located(By.CLASS_NAME('button mainaction submit'))).click()

find element by id should work. If you need to wait for the page to load, you can use the EC.presence_of_element_located. Don't set hardcoded wait time.

   ` try:
        element_present = EC.presence_of_element_located((By.ID, 'rcmloginuser'))
        WebDriverWait(driver, timeout).until(element_present)
        user_name = driver.find_element_by_id('rcmloginuser')
        pass_word = driver.find_element_by_id('rcmloginpwd')
        user_name.send_keys(user)
        pass_word.send_keys(password)
        submit_1 = driver.find_element_by_id('rcmloginsubmit h_webmail-login_page-login_button')
        submit_1.click()
    except TimeoutException:
        sys.exit(1) `

IMPORTANT: Check this answer if you can't open an element AFTER opening a new Tab. This was my problem *

72 tries and about 5 hours later i finally found the answer. None of the answers above helped me, perhaps because i wasn't clear enough.

Basically, the first part of my code opened a window, inserted password, email etc and everything went fine. Then, i opened a new tab with>(which someone here claimed to be Javascript. It may be, but it is definitely a command in python too.)

driver.execute_script("window.open('https://webmail1.hostinger.com/');)

I tried finding elements from this new tab that had been opened, but you need to tell the webdriver to switch to the new tab. I thought only opening it was enough for the webdriver to automatically swith to it. Basically, i was searching for elements of tab 2 in tab 1's html, and that is why i wasn't finding them. Here is my code now:

#opens new tab
driver.execute_script("window.open('https://webmail1.hostinger.com/');")
#switches to the second tab 
driver.switch_to.window(driver.window_handles[1])
driver.get(tab_url)

email= driver.find_element_by_name('_user')
email.send_keys("my_email")

password = driver.find_element_by_id("rcmloginpwd")
password.send_keys("my_password")

It works like a charm. This is the link were i found about this. Go there for more information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM