gpt4 book ai didi

python - Selenium Python - 如何将 key 发送到网络表单

转载 作者:太空宇宙 更新时间:2023-11-03 10:52:52 25 4
gpt4 key购买 nike

我正在尝试使用 selenium 填写网络表单。虽然我能够成功填充大多数文本框并选择单选按钮,但在尝试将 key 发送到三个[出生日期] 字段(日、月和年)时,我收到以下异常以及表单第二部分中的 [lifecover] 字段。

ElementNotVisibleException: element not visible (Session info: chrome=62.0.3202.62) (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.1.7601 SP1 x86_64)

上述异常是从以下代码接收到的:

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

path_to_chromedriver = 'C:/Users/Me/chromedriver/chromedriver'
browser = webdriver.Chrome(executable_path = path_to_chromedriver)

url = 'https://lifeinsurance.rac.com.au/rac/get-a-quote?productid=51'
browser.get(url)

...

# date of birth - CANNOT POPULATE!
browser.find_element_by_id('txtDateOfBirth__1_Day').send_keys('01')
browser.find_element_by_id('txtDateOfBirth__1_Month').send_keys('06')
browser.find_element_by_id('txtDateOfBirth__1_Year').send_keys('1988')

...

# cover amount - CANNOT POPULATE!
browser.find_element_by_xpath('//*[@id="txtCoverAmount__1"]').send_keys('150000')

...

完整代码(包含以上内容)如下:

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

path_to_chromedriver = 'C:/Users/Me/chromedriver/chromedriver'
browser = webdriver.Chrome(executable_path = path_to_chromedriver)

url = 'https://lifeinsurance.rac.com.au/rac/get-a-quote?productid=51'
browser.get(url)

# quote type
browser.find_element_by_xpath('//*[@id="rdApplicantCount_1_Label"]/span[1]').click()

# title
browser.find_element_by_xpath('//*[@id="divApplicantDetails__1"]/div[1]/div/div[2]/span/span/span[1]').click()
browser.find_element_by_xpath('//*[@id="ddTitle__1_listbox"]/li[2]').click()

# first name and surname
browser.find_element_by_name('txtFirstName__1').send_keys('richard')
browser.find_element_by_name('txtSurname__1').send_keys('castle')

# email
browser.find_element_by_name('txtEmail__1').send_keys('rcastle@gmail.com')

# mobile number
browser.find_element_by_name('txtPhoneNumber__1').send_keys('0418545898')

# date of birth - CANNOT POPULATE!
browser.find_element_by_id('txtDateOfBirth__1_Day').send_keys('01')
browser.find_element_by_id('txtDateOfBirth__1_Month').send_keys('06')
browser.find_element_by_id('txtDateOfBirth__1_Year').send_keys('1988')

# gender
browser.find_element_by_xpath('//*[@id="rdGender_Female__1_Label"]/span[1]').click()

# residence
browser.find_element_by_xpath('//*[@id="divApplicantDetails__1"]/div[8]/div/div[2]/span/span/span[1]').click()
browser.find_element_by_xpath('//*[@id="ddResidentialStatusId__1_listbox"]/li[1]').click()

# residing in australia
browser.find_element_by_xpath('//*[@id="rdResidingInAustralia_Yes__1_Label"]/span[1]').click()

# open cover section
browser.find_element_by_xpath('//*[@id="divQuote_Accordion"]/div[3]/div').click()

# tobacco
browser.find_element_by_xpath('//*[@id="rdSmoker_Yes__1_Label"]/span[1]').click()

# cover amount - CANNOT POPULATE!
browser.find_element_by_xpath('//*[@id="txtCoverAmount__1"]').send_keys('150000')

# get quote
browser.find_element_by_xpath('//*[@id="divApplicantCover"]/div[3]/div[2]/button').click()

# premium frequency
browser.find_element_by_xpath('//*[@id="productTabStrip-1"]/div[1]/div[1]/div/div[4]/div[2]/span/span/span[1]').click()
browser.find_element_by_xpath('//*[@id="ddPremiumFrequencyId_listbox"]/li[2]').click()

# capture result
browser.find_element_by_xpath('//*[@id="productTabStrip-1"]/div[1]/div[1]/div/div[3]/div[1]/div[2]/span').text

我是 selenium 的新手,如果有人可以建议如何将 key 发送到引发异常的字段,我将不胜感激

最佳答案

如果您检查 input 字段的 HTML 源代码,您会看到它具有 style="display: none;" 属性,这意味着您无法与这个元素即使实现显式/隐式等待...要使其可见,请尝试将 style="display: none;" 更改为 style="display: inline;"如下:

day = browser.find_element_by_id('txtDateOfBirth__1_Day')
browser.execute_script('arguments[0].style.display="inline";', day)
day.send_keys('01')

对其他两个输入字段执行相同的操作

关于python - Selenium Python - 如何将 key 发送到网络表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46908505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com