【发布时间】:2020-02-14 03:17:18
【问题描述】:
我正在尝试使用 python 和 selenium 构建一个自动化脚本。这是到目前为止的代码。出于隐私原因,我将敏感信息设为空白。我基本上希望处理单元框自动填充处理单元变量。
from selenium import webdriver
from selenium.webdriver.common.by import By
OriginZip = 99501
DestinationZip = 94016
HandlingUnit = 1
driver = webdriver.Chrome(r"C:\Users\*******\Desktop\WebAutomation\chromedriver")
driver.maximize_window()
driver.get("***********")
driver.implicitly_wait(1)
#Login Page
driver.find_element(By.XPATH, "//button[@id='dropdownMenuButton']").click()
driver.find_element(By.XPATH, "//input[@id='UserName']").send_keys('*******')
driver.find_element(By.XPATH, "//input[@id='Password']").send_keys('*******')
driver.find_element(By.XPATH, "//button[@class='btn btn-primary btn-block']").click()
#2nd Page
driver.find_element(By.XPATH, "//input[@id='OriginZip']").send_keys(OriginZip)
driver.find_element(By.XPATH, "//input[@id='DestinationZip']").send_keys(DestinationZip)
driver.find_element(By.XPATH, "//button[@class='btn btn-secondary form-control']").click()
#3rd Page
driver.refresh() #<-- Add this to fix
driver.find_element(By.XPATH, "//input[@id='handlingunits']").send_keys(HandlingUnit) #here is where the error occurs
出现的错误:
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=80.0.3987.87)
我尝试添加一个implicity_wait 方法,以及按ID 查找元素。有人知道这个问题的正确解决方案吗?
【问题讨论】:
-
请阅读为什么是screenshot of HTML or code or error is a bad idea。考虑使用基于格式化文本的相关 HTML 和错误堆栈跟踪来更新问题。
标签: python-3.x selenium selenium-webdriver xpath