使用Chromedriver Python的Selenium:无法定位元素:{“方法”:“cs s选择器”,“选择器”:“[id=”password“]”}
我将问题编辑为:使用chrome浏览器时效果很好,但同样的代码在--headless模式下失败(这是我想要使用的)
在过去的一周里,我一直在为这个问题挠头: Chromedriver无法找到位于((By.ID,'password'))的元素的存在,尽管它在屏幕截图中清楚地显示在那里。 它通过相同的by.ID找到用户名并正确输入,但是当它找到w密码时,它失败了
[******@mtlab自动tmp]$google chrome——版本
谷歌浏览器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\"]"
}
在您的某些情况下,我面临的一些问题:
希望有用
要在密码字段内发送字符序列,必须为
element_to_be_clickable()
引入WebDriverWait,并且可以使用以下Locator Strategies之一:使用
CSS_SELECTOR
:使用
XPATH
:注意:您必须添加以下导入:
浏览器快照:
相关问题 更多 >
编程相关推荐