Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python selenium无法在iframe中找到输入对象_Python_Selenium_Iframe - Fatal编程技术网

Python selenium无法在iframe中找到输入对象

Python selenium无法在iframe中找到输入对象,python,selenium,iframe,Python,Selenium,Iframe,我的目标是在使用selenium时自动填写表单 我可以填写密码,但我仍在努力填写用户名 用户名字段位于iframe中。所述iframe的HTML如下所示: <iframe title="Registration form" scrolling="no" class="top" data-name="top" sandbox="allow-scripts allow-same-origin allow-

我的目标是在使用selenium时自动填写表单

我可以填写密码,但我仍在努力填写用户名

用户名字段位于iframe中。所述iframe的HTML如下所示:

<iframe title="Registration form" scrolling="no" class="top" data-name="top" sandbox="allow-scripts allow-same-origin allow-popups allow-top-navigation" src="https://secure.protonmail.com/abusev2.iframe.html?name=top">
    #document
        <!DOCTYPE html>
        <html lang="en">
        <head>...</head>
        <body>
            <div id="app" data-name="top">
                <div class="formList">
                    <div class="field field-usernameInput">
                        <div class="group-username">
                            <label for="username">Choose username</label>
                            <input placeholder="Choose username" required="" name="username" messages="[object Object]" iframename="top" pattern=".{1,40}" id="username" class="input">
                            <div class="field field-select"><label for="domain">Select a domain</label><div class="select-mask"><select component="domains" name="domain" id="domain"><option value="protonmail.com">protonmail.com</option><option value="protonmail.ch">protonmail.ch</option></select><i>▼</i></div></div>
                        </div>
                    </div>
                </div>
            </div>
    
    </iframe>
我也不例外。 但是,我现在找不到要执行的输入对象。send_keys()

我试过了

driver.find_element_by_id('username')
driver.find_element_by_class_name('input')
driver.find_element_by_tag_name('input')
driver.find_element_by_name('username')
driver.find_element_by_xpath('//input[@id="username"]')
driver.find_element_by_css_selector('#username')
但每次我都会遇到同样的例外:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: *whatever I searched for*
当我试着

driver.find_element_by_id('app') #first div object in the iframe
我没有得到任何例外。 如果我不切换到iframe,我就找不到这一点,这让我相信切换到iframe是有效的。 但是,我无法使用上述任何方法找到任何其他div对象


总而言之:我不知道为什么我找不到selenium的输入对象来自动添加用户名后缀,我非常感谢关于我做错了什么的想法。

这将使第一个iframe使用更有针对性的xpath

driver.switch_to.frame(driver.find_element_by_xpath("(//iframe[@title='Registration form'])[1]"))
因此,要以第一个iframe为目标,等待用户名并向其发送密钥

wait = WebDriverWait(driver, 10)
driver.get('https://mail.protonmail.com/create/new?language=en')
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"(//iframe[@title='Registration form'])[1]")))
wait.until(EC.element_to_be_clickable((By.XPATH,"(//div[@class='group-username'])[1]/input"))).send_keys('a')
进口

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

我已经实施了你的建议,现在它正在发挥作用。我不确定我是否完全理解这个解决方案。你说“这会让第一个iframe使用更有针对性的xpath。”这是否意味着我没有正确地切换到iframe?为什么我能够在iframe中找到第一个div对象而没有找到任何其他对象?其余的元素不在iframe中。像密码等,我看到密码字段不在iframe中。由于输入对象介于。。。就像我能够找到的div对象一样,我假设切换到iframe是成功的。我认为通过它的tag_名称找到它已经足够好了,因为它是第一个提到的。我想我错了。将您的建议与By.XPATH或By.CSS_选择器一起使用修复了该问题。谢谢你的帮助。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC