Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python selenium在一行的3个网页上填写答案表_Python_Selenium - Fatal编程技术网

python selenium在一行的3个网页上填写答案表

python selenium在一行的3个网页上填写答案表,python,selenium,Python,Selenium,我正在使用下面的代码填写网页上的答案表。如果在字典中找到一个键,它将用相应的值填充答案表单: 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:

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

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

循环分页。把你的代码放进去