python - 如何使用 Selenium 和 Python 将文本发送到密码字段
问题描述
Selenium 与 Chromedriver Python:无法找到元素:{“method”:“cs s 选择器”,“selector”:“[id="password"]"}
我编辑了问题说,它在使用 chrome 浏览器时运行良好,但相同的代码在 --headless 模式下失败(这是我想要使用的)
在过去的一周里,我一直在为这个问题摸不着头脑:Chromedriver 找不到present_of_element_located((By.ID, 'password')),尽管它在屏幕截图中明显存在。它通过相同的 By.ID 找到用户名并正确输入,但是当它获得密码时,它会失败。
[****@mtlab-auto tmp]$ google-chrome --version
谷歌浏览器 83.0.4103.106
无头镀铬=83.0.4103.106
Python 3.8.0(默认,2020 年 5 月 7 日,02:49:39)
########### Chrome option ###############
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--headless')
chromeOptions.add_argument('window-size=1920,1080')
chromeOptions.add_argument('start-maximized')
chromeOptions.add_argument('disable-infobars')
prefs = {'download.default_directory' : f'{temp_folder}'}
chromeOptions.add_experimental_option('prefs',prefs)
# capabilities = DesiredCapabilities.CHROME.copy()
# capabilities['acceptSslCerts'] = True
# capabilities['acceptInsecureCerts'] = True
############# Set chrome proxy ###########
Chrome_Proxy = 'http://135.28.13.11:8888'
#Chrome_Proxy = 'http://autoproxy.sbc.com/autoproxy.cgi'
chromeOptions.add_argument('--proxy-server=%s' % Chrome_Proxy)
############### Enable chrome browser wedriver ##############
browser = webdriver.Chrome(executable_path='/home/dev/chromedriver', options=chromeOptions,\
service_args = [f'--log-path={chromeDriver_log}'])
# browser = webdriver.Chrome(executable_path='/home/dev/chromedriver', options=chromeOptions,\
# desired_capabilities = capabilities, service_args = [f'--log-path={chromeDriver_log}'])
############## Set the timeout values ###############
ignored_exceptions = (NoSuchElementException,StaleElementReferenceException)
#browser.set_page_load_timeout(15)
#browser.implicitly_wait(40)
element_timeout = 30
print()
print(Fore.BLUE + '>>> Accessing Cisco Cloud...')
browser.get(r'https://slexui.cloudapps.cisco.com/SWIFT/LicensingUI/Quickstart')
time.sleep(20)
browser.get_screenshot_as_file('/tmp/main-page.png')
try:
element_login = EC.presence_of_element_located((By.ID, 'login-button'))
WebDriverWait(browser, element_timeout).until(element_login)
except TimeoutException:
print(Fore.RED + 'Fail to load login page')
print('RC=101,"Error: Failed to load Cisco Website"')
browser.close()
quit()
print()
print(browser.title)
print()
######### Login to cisco cloud account ###########
print(Fore.BLUE + ' >>> Login to Cisco Cloud')
######################## Username ##################
try:
element_username = EC.presence_of_element_located((By.ID, 'userInput'))
WebDriverWait(browser, element_timeout).until(element_username).send_keys(f'{cisco_userid}')
except TimeoutException:
print(Fore.RED + 'Fail to load login page')
print('RC=101,"Error: Failed to login to Cisco Website"')
browser.close()
quit()
time.sleep(2)
#browser.find_element_by_id('userInput').send_keys(f'{cisco_userid}')
print('>>>> Username input')
browser.find_element_by_id('login-button').click()
time.sleep(20)
browser.get_screenshot_as_file('/tmp/password.png')
###################### Password ##################
try:
element_password = EC.presence_of_element_located((By.ID, 'password'))
WebDriverWait(browser, element_timeout).until(element_password).send_keys(f'{cisco_password}')
except TimeoutException:
print(Fore.RED + 'Timed out waiting for page to load')
print('RC=101,"Error: Failed to login to Cisco Website"')
browser.close()
quit()
time.sleep(5)
#browser.find_element_by_id('password').send_keys(f'{cisco_password}')
print('>>>> Password input')
browser.find_element_by_id('kc-login').click()
############# Check if we are logged in #############
这是日志:它找到我的用户名并输入正确的用户名,但在密码字段中失败。输入用户名后立即截图,可以清楚地看到密码字段,chromedriver没有找到任何内容
[1592922143.110][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND Navigate {
"url": "https://slexui.cloudapps.cisco.com/SWIFT/LicensingUI/Quickstart"
}
[1592922143.110][INFO]: Waiting for pending navigations...
[1592922143.111][INFO]: Done waiting for pending navigations. Status: ok
[1592922168.003][INFO]: Waiting for pending navigations...
[1592922243.956][INFO]: Done waiting for pending navigations. Status: ok
[1592922243.956][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE Navigate
[1592922263.980][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND Screenshot {
}
[1592922263.980][INFO]: Waiting for pending navigations...
[1592922263.982][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.304][INFO]: Waiting for pending navigations...
[1592922264.305][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.305][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE Screenshot "iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAYAAADo08FDAAAAAXNSR0IArs4c6Q
AAIABJREFUeJzs3XecnWWZN/DfmTnTMsmkzARCEpDQErq0QABBREEhSHEtKJaAq7irK7Iq+y7i6loWQRfbWt5XkSILWEFBikgRCIjSCaFICAgJ6clMMpPp7x+DE0ISQpKBgYfv9..."
[1592922264.309][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
"using": "css selector",
"value": "[id=\"login-button\"]"
}
[1592922264.309][INFO]: Waiting for pending navigations...
[1592922264.310][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.329][INFO]: Waiting for pending navigations...
[1592922264.330][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.330][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE FindElement {
"element-6066-11e4-a52e-4f735466cecf": "0db6e420-49df-46ab-9939-b2f471f9d919"
}
[1592922264.331][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND GetTitle {
}
[1592922264.331][INFO]: Waiting for pending navigations...
[1592922264.332][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.335][INFO]: Waiting for pending navigations...
[1592922264.336][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.336][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE GetTitle "Cisco.com Login Page"
[1592922264.337][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
"using": "css selector",
"value": "[id=\"userInput\"]"
}
[1592922264.337][INFO]: Waiting for pending navigations...
[1592922264.338][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.355][INFO]: Waiting for pending navigations...
[1592922264.356][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.356][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE FindElement {
"element-6066-11e4-a52e-4f735466cecf": "72de3929-e23d-436c-ab22-adb6e79109bd"
}
[1592922264.357][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND TypeElement {
"id": "72de3929-e23d-436c-ab22-adb6e79109bd",
"text": "an144a001",
"value": [ "*", "*", "*", "*", "*", "*", "0", "0", "1" ]
}
[1592922264.357][INFO]: Waiting for pending navigations...
[1592922264.358][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.496][INFO]: Waiting for pending navigations...
[1592922264.497][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.497][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE TypeElement
[1592922266.502][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
"using": "css selector",
"value": "[id=\"login-button\"]"
}
[1592922266.502][INFO]: Waiting for pending navigations...
[1592922266.504][INFO]: Done waiting for pending navigations. Status: ok
[1592922266.517][INFO]: Waiting for pending navigations...
[1592922266.518][INFO]: Done waiting for pending navigations. Status: ok
[1592922266.518][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE FindElement {
"element-6066-11e4-a52e-4f735466cecf": "0db6e420-49df-46ab-9939-b2f471f9d919"
}
[1592922266.521][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND ClickElement {
"id": "0db6e420-49df-46ab-9939-b2f471f9d919"
}
[1592922266.521][INFO]: Waiting for pending navigations...
[1592922266.522][INFO]: Done waiting for pending navigations. Status: ok
[1592922266.622][INFO]: Waiting for pending navigations...
[1592922266.624][INFO]: Done waiting for pending navigations. Status: ok
[1592922266.624][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE ClickElement
[1592922286.634][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND Screenshot {
}
[1592922286.634][INFO]: Waiting for pending navigations...
[1592922286.636][INFO]: Done waiting for pending navigations. Status: ok
[1592922287.079][INFO]: Waiting for pending navigations...
[1592922287.080][INFO]: Done waiting for pending navigations. Status: ok
[1592922287.080][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE Screenshot "iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAYAAADo08FDAAAAAXNSR0IArs4c6Q
AAIABJREFUeJzs3XmYnFWdL/BvVVd3p7uTzk72QAiBQFiCsqNsAVzhDoIoijqIo86AOnp1HK+z4DIzOug4V8dt0FHwKqCAsiggKosgEER22RMEAiEL2Tvpte4fkQ6dfemk4OXze..."
[1592922287.087][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
"using": "css selector",
"value": "[name=\"password\"]"
}
[1592922287.087][INFO]: Waiting for pending navigations...
[1592922287.088][INFO]: Done waiting for pending navigations. Status: ok
[1592922287.109][INFO]: Waiting for pending navigations...
[1592922287.110][INFO]: Done waiting for pending navigations. Status: ok
[1592922287.110][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE FindElement ERROR no such element: Unable to locate element: {"method":"cs
s selector","selector":"[name="password"]"}
(Session info: headless chrome=83.0.4103.106)
[1592922287.614][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
"using": "css selector",
"value": "[name=\"password\"]"
}
[1592922287.614][INFO]: Waiting for pending navigations...
[1592922287.615][INFO]: Done waiting for pending navigations. Status: ok
[1592922287.628][INFO]: Waiting for pending navigations...
[1592922287.629][INFO]: Done waiting for pending navigations. Status: ok
[1592922287.629][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE FindElement ERROR no such element: Unable to locate element: {"method":"cs
s selector","selector":"[name="password"]"}
(Session info: headless chrome=83.0.4103.106)
[1592922288.132][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
"using": "css selector",
"value": "[name=\"password\"]"
}
[1592922288.132][INFO]: Waiting for pending navigations...
[1592922288.133][INFO]: Done waiting for pending navigations. Status: ok
[1592922288.145][INFO]: Waiting for pending navigations...
[1592922288.146][INFO]: Done waiting for pending navigations. Status: ok
[1592922288.146][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE FindElement ERROR no such element: Unable to locate element: {"method":"cs
s selector","selector":"[name="password"]"}
(Session info: headless chrome=83.0.4103.106)
[1592922288.649][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
"using": "css selector",
"value": "[name=\"password\"]"
}
[1592922288.649][INFO]: Waiting for pending navigations...
[1592922288.650][INFO]: Done waiting for pending navigations. Status: ok
[1592922288.662][INFO]: Waiting for pending navigations...
[1592922288.663][INFO]: Done waiting for pending navigations. Status: ok
[1592922288.663][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE FindElement ERROR no such element: Unable to locate element: {"method":"cs
s selector","selector":"[name="password"]"}
(Session info: headless chrome=83.0.4103.106)
[1592922289.166][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
"using": "css selector",
"value": "[name=\"password\"]"
}
解决方案
要在密码字段中发送字符序列,您必须诱导WebDriverWait并且您可以使用以下任一定位器策略:element_to_be_clickable()
使用
CSS_SELECTOR
:driver.get('https://slexui.cloudapps.cisco.com/SWIFT/LicensingUI/Quickstart') WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#userInput"))).send_keys("an144a001") driver.find_element_by_css_selector("input#login-button").click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#password"))).send_keys("Checksum321")
使用
XPATH
:driver.get('https://slexui.cloudapps.cisco.com/SWIFT/LicensingUI/Quickstart') WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='userInput']"))).send_keys("an144a001") driver.find_element_by_xpath("//input[@id='login-button']").click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='password']"))).send_keys("Checksum321")
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
浏览器快照:
推荐阅读
- solr - 没有日期字段的 Apache Nifi GetSolr 配置
- spring-boot - 在 SpringBoot 应用程序中缓存 OAuth2 令牌?
- reactjs - 尝试使用断点,但我在开发人员工具上的代码为红色,无法将鼠标悬停在值上
- react-native - 当我尝试开始博览会时 ENOENT
- django - django 无法链接用户模型和相应的 from 以在模板中用于注册
- javascript - 如何使用excelJs设置导出文件的标题
- sql - 具有条件优先级的 SQL 条件连接,例如 CASE 语句
- c++ - 从 cpu 代码到 gpu 着色器获取正确的漫反射材质
- python - 从列表中删除最后一次出现的包含子字符串的元素
- ruby-on-rails - 乘客无法加载咖啡轨宝石