如何使用selenium和Python查找元素Internet Explorer

时间:2017-04-03 12:45:46

标签: python python-2.7 selenium internet-explorer selenium-webdriver

我正在尝试将文本输入登录页面。登录页面是: https://ppair.uspto.gov/TruePassSample/AuthenticateUserLocalEPF.html

在Internet Explorer中使用“检查元素”(该网站仅在Internet Explorer中加载)在我看来,“选择数字证书”文本字段的名称是:“username”

这是我的剧本:

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# Digital Certificate Path
DigitalCertificateFolder = 'C:\FOLDER'
DigitalCertificateFile = 'FILE.epf'
DigitalCertificatePath = DigitalCertificateFolder + '\\' + DigitalCertificateFile

password = 'PASSWORD'

# get the path of IEDriverServer
dir = 'C:\FOLDER2'
ie_driver_path = dir + "\IEDriverServer.exe"

# create a new Internet Explorer session
driver = webdriver.Ie(ie_driver_path)
driver.implicitly_wait(30)
driver.maximize_window()

# navigate to the application home page
driver.get("https://ppair.uspto.gov/TruePassSample/AuthenticateUserLocalEPF.html")

# get the search textbox
Select_Digital_Certificate = driver.find_element_by_name("username")
Select_Digital_Certificate.send_keys(DigitalCertificatePath)

这是Internet Explorer中inspect元素的输出:

<INPUT name=username style="CURSOR: auto; BACKGROUND-IMAGE: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAASCAYAAABSO15qAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3QsPDhss3LcOZQAAAU5JREFUOMvdkzFLA0EQhd/bO7iIYmklaCUopLAQA6KNaawt9BeIgnUwLHPJRchfEBR7CyGWgiDY2SlIQBT/gDaCoGDudiy8SLwkBiwz1c7y+GZ25i0wnFEqlSZFZKGdi8iiiOR7aU32QkR2c7ncPcljAARAkgckb8IwrGf1fg/oJ8lRAHkR2VDVmOQ8AKjqY1bMHgCGYXhFchnAg6omJGcBXEZRtNoXYK2dMsaMt1qtD9/3p40x5yS9tHICYF1Vn0mOxXH8Uq/Xb389wff9PQDbQRB0t/QNOiPZ1h4B2MoO0fxnYz8dOOcOVbWhqq8kJzzPa3RAXZIkawCenHMjJN/+GiIqlcoFgKKq3pEMAMwAuCa5VK1W3SAfbAIopum+cy5KzwXn3M5AI6XVYlVt1mq1U8/zTlS1CeC9j2+6o1wuz1lrVzpWXLDWTg3pz/0CQnd2Jos49xUAAAAASUVORK5CYII=); BACKGROUND-REPEAT: no-repeat; BACKGROUND-ATTACHMENT: scroll; BACKGROUND-POSITION: right center" type=text size=38 lpcachedvisval="1" lpcachedvistime="1491220212">

当我尝试在控制台中运行脚本以接收以下错误时:“NameError:name'Select_Digital_Certificate'未定义”。

有人可以向我解释我做错了吗?

1 个答案:

答案 0 :(得分:1)

位于iframe内的必填输入字段,因此您需要在处理iframe之前切换到input

driver.get("https://ppair.uspto.gov/TruePassSample/AuthenticateUserLocalEPF.html")
driver.switch_to.frame('entrustTruePassGuiFrame')
Select_Digital_Certificate = driver.find_element_by_name("username")
...

要切换回主HTML文档,您可能需要使用

driver.switch_to.default_content()