简体   繁体   English

我在 Selenium 中找不到元素。(Python/Google)

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

This is the link https://webmail1.hostinger.com/这是链接https://webmail1.hostinger.com/

I want to locate the 2 input boxes and input my email and password, but i can't acess them.我想找到 2 个输入框并输入我的电子邮件和密码,但我无法访问它们。 I can easily find them by inspecting the page but i can't find them by their xpath, selector, id etc.我可以通过检查页面轻松找到它们,但我无法通过它们的 xpath、选择器、id 等找到它们。

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. “execute_script”是一种javascript方法,用于查找用户可能无法访问的元素,用于查找元素而不是打开网站。 Ive also found the id of the input boxes.我还找到了输入框的 id。

use driver.get(URL) instead改用 driver.get(URL)

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.如果你想访问一个 URL 并获取 id 并在 python 中提交,你应该做这样的事情。 Your tags were not in an iframe and we use webdriver waits instead of time.sleep()您的标签不在 iframe 中,我们使用 webdriver 等待而不是 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.按 id 查找元素应该可以工作。 If you need to wait for the page to load, you can use the EC.presence_of_element_located.如果需要等待页面加载,可以使用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. 72 次尝试,大约 5 小时后我终于找到了答案。 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.)然后,我用>打开了一个新标签(这里有人声称是Javascript。可能是,但它绝对是python中的一个命令。)

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.我尝试从这个已打开的新选项卡中查找元素,但您需要告诉 webdriver 切换到新选项卡。 I thought only opening it was enough for the webdriver to automatically swith to it.我认为只要打开它就足以让 webdriver 自动切换到它。 Basically, i was searching for elements of tab 2 in tab 1's html, and that is why i wasn't finding them.基本上,我在标签 1 的 html 中搜索标签 2 的元素,这就是我没有找到它们的原因。 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去那里了解更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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