簡體   English   中英

如何使用 Selenium 和 Python 將文本發送到密碼字段

[英]How to send text to the Password field using Selenium and Python

Selenium 與 Chromedriver Python: 無法定位元素: {"method":"cs s selector","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\"]"
}

在此處輸入圖像描述

在此處輸入圖像描述

要在密碼字段中發送字符序列,您必須為element_to_be_clickable()引入WebDriverWait ,並且您可以使用以下任一定位器策略

  • 使用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
  • 瀏覽器快照:

身份_思科

我在您的某些情況下遇到的一些問題:

  • 頁面的 html 使用無頭模式加載不同,它沒有找到一些元素。 嘗試使用它評論無頭模式。
  • 嘗試以不同的方式找到它,即使用 find_element_by_xpath 或 class_name。
  • 嘗試使用交互式控制台,在selenium加載的瀏覽器windows中獲取密碼元素的完整路徑並執行命令`find_element_by_xpath(full_path)。 它應該可以工作,如果不嘗試交互所有方法,然后在您的代碼中進行相應更改。

希望有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM