在selenium中按下按钮后刷新页面?

628mspwn  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(589)

嗨,我有这段代码,在点击提交按钮后,雅虎上尝试了很多次。页面似乎被刷新了,因为我不能按相同的按钮。
这是我的密码

import os
from tkinter import *
from tkinter import filedialog
import selenium
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium import webdriver
from selenium import webdriver
import contextlib as textmanager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

PATH= "C:\chromedrivers\chromedriver.exe"
driver= webdriver.Chrome(PATH)

list=[]
file= open("numbers.txt", "r")

for line in file:
        line = line.strip() #preprocess line
        list.append(line)

driver.get("https://www.yahoo.com/")

element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='consent-page']/div/div/div/form/div[2]/div[2]/button")))

element.click();

time.sleep(1)

sign_in= driver.find_element_by_xpath("//*[@id='ybar-inner-wrap']/div[3]/div/div[3]/div[1]/div/a").click()
time.sleep(1)
forgot_phone= driver.find_element_by_xpath("//*[@id='mbr-forgot-link']").click()

time.sleep(2)
inputt= driver.find_element_by_id("username")
time.sleep(2)
buttonn= driver.find_element_by_xpath("//*[@id='yid-challenge']/form/div[2]/button")

counter=0

nonumber= open("nonumber.txt","w")

while counter != len(list):
    inputt.send_keys(list[counter])
    time.sleep(1)
    buttonn.click()
    if len(driver.find_elements_by_css_selector("p[data-error]")) > 0 :
        inputt.clear()
        nonumber.write(list[counter])
        nonumber.close()
    counter=counter+1

这是一个错误,它说按钮不可点击,但在第一次运行它的作品我不确定是在按钮或输入区的问题

Traceback (most recent call last):
  File "c:\Users\ramhelsinki\projects\slenium.py", line 54, in <module>
    inputt.send_keys(list[counter])
  File "C:\Users\ramhelsinki\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT,
  File "C:\Users\ramhelsinki\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\ramhelsinki\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\ramhelsinki\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=91.0.4472.124)
koaltpgm

koaltpgm1#

每次更改/刷新网页时,selenium以前获取的所有web元素都变得无关。这是什么 stale element reference 这是一个例外。
对于您的代码,由于页面在输入错误时会刷新,因此我建议在每次更改页面后再次获取元素,因此代码如下所示:

time.sleep(2)

counter=0

nonumber= open("nonumber.txt","w")

while counter != len(list):
    inputt= driver.find_element_by_id("username")
    buttonn= driver.find_element_by_xpath("//button")

    inputt.send_keys(list[counter])
    time.sleep(1)
    buttonn.click()
    if len(driver.find_elements_by_css_selector("p[data-error]")) > 0 :
        inputt= driver.find_element_by_id("username")
        inputt.clear()
        nonumber.write(list[counter])
        nonumber.close()
    counter=counter+1

相关问题