【问题标题】:Page refreshing after pressing button in selenium?在硒中按下按钮后页面刷新?
【发布时间】:2021-07-10 21:27:29
【问题描述】:

您好,点击提交按钮后,我在 yahoo 上有此代码尝试了很多数字 页面似乎刷新了,因为我无法按下相同的按钮。

这是我的代码

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)

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    每次更改/刷新网页时,Selenium 之前获取的所有网页元素都变得无关紧要。这就是 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      相关资源
      最近更新 更多