gpt4 book ai didi

python selenium在连续3个网页上填写答案表

转载 作者:行者123 更新时间:2023-12-01 01:32:13 25 4
gpt4 key购买 nike

我正在使用下面的代码在网页上填写答案表格。如果在字典中找到一个键,它就会用相应的值填充答案表单:

secuQA = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6}
q_element = browser.find_element_by_id("secu_ques")
question_strings = q_element.text.split(" ")

for key in secuQA:
if key in question_strings:
ans = secuQA[key]
ansElem = browser.find_element_by_id("secu_answ")
ansElem.click()
ansElem.send_keys(ans)
ansElem.send_keys(Keys.ENTER)
break

3 个这样的页面连续出现(格式相同,只是随机问题不同)。如何让代码填写所有 3 页上的答案表格?

最佳答案

您可以循环遍历所有页面,添加一些等待语句以使您的代码更加健壮。大致如下:

#necessary imports 
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


secuQA = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6}


for question in range(3): # loop through 3 pages

WebDriverWait(browser, 20).until(
EC.presence_of_element_located((By.ID, "secu_ques"))) # wait until the question has been loaded on the page

q_element = browser.find_element_by_id("secu_ques")
question_strings = q_element.text.split(" ")

for key in secuQA:
if key in question_strings:
ans = secuQA[key]
ansElem = browser.find_element_by_id("secu_answ")
ansElem.click()
ansElem.send_keys(ans)
ansElem.send_keys(Keys.ENTER)
break

time.sleep(1)#lets wait a while to the next page to load

关于python selenium在连续3个网页上填写答案表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52750860/

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