英文:
Selenium *appears* to be ignoring waits and running over itself
问题
Selenium 似乎 忽略了等待并且在执行过程中重叠
我正在使用 Selenium 与 Python(Chrome 驱动程序)来迭代一个术语列表,在我的网站上搜索它们并提取一些文本。
然而,我发现脚本没有正确等待,而是在上一次搜索有足够时间处理之前就输入了术语,导致一堆术语一次性粘贴。我怀疑问题不是 Selenium 出了问题,更可能只是我误解了,但我尝试了我能想到的所有可能有所帮助的等待类型。
这里是处理 Selenium 部分的代码。还有另一个函数,初始化 Chrome 驱动程序,然后为每个术语调用下面的函数。
def searchTerm(driver, term): # 在页面上搜索术语参数,返回特定文本
wait = WebDriverWait(driver, 10)
try:
element = wait.until(EC.presence_of_element_located((By.ID, "search-box"))) # 等待直到元素出现在页面上(不一定可见)
element.clear() # 清除任何现有文本
element.send_keys(term) # 将字符串参数 term 发送到搜索框
wait.until(EC.text_to_be_present_in_element_value((By.ID, "search-box"), term)) # 再次检查术语实际上在搜索框中
element.send_keys(Keys.ENTER) # 按 Enter 键进行搜索
wait.until(EC.text_to_be_present_in_element_value((By.ID, "search-box"), "")) # 在这个网站上,搜索框内容在执行搜索时被清空。检查框是否为空作为验证页面已加载的手段。这可能是不必要的
element = wait.until(EC.presence_of_element_located((By.XPATH,"//*[@class='target-text-class']",))) # 验证我们想要的文本是否存在
return element.text # 返回文本
except Exception as er:
print(er)
return "ERROR"
它经常在 send_keys(term)
上抛出 StaleReferenceException
,这让我感到困惑,因为在它之前的几行验证元素是否存在的代码没有抛出任何错误。我尝试删除一些等待部分并尝试添加更多等待,但结果都一样。
英文:
Selenium appears to be ignoring waits and running over itself
I'm using Selenium with Python (Chrome driver) to iterate through a list of terms, search them on my site, and grab some text.
However, I'm finding that the script is not properly waiting, and is entering terms before the previous search has had time to process, leading to a bunch of terms being pasted at once. I doubt the issue is selenium being broken, and is more likely just my misunderstanding, but I've tried every type of wait I could think of that would help.
Here is the code that handles the selenium stuff. There is another function that initializes the Chrome driver and then just calls the function below for each term.
def searchTerm(driver, term): # searching term argument on the page, returns specific text
wait = WebDriverWait(driver, 10)
try:
element = wait.until(EC.presence_of_element_located((By.ID, "search-box"))) # wait until element is present in page (not necessarily visible)
element.clear() #clear any pre-existing text
element.send_keys(term) #send string arugment term into search box
wait.until(EC.text_to_be_present_in_element_value((By.ID, "search-box"), term)) #double check term is actually in search box
element.send_keys(Keys.ENTER) #press enter to search
wait.until(EC.text_to_be_present_in_element_value((By.ID, "search-box"), "")) #on this site, search box contents are emptied when search is performed. check box is empty as means of verifying page has loaded. this probably isn't necessary
element = wait.until(EC.presence_of_element_located((By.XPATH,"//*[@class='target-text-class']",))) #verify the text we want is present
return element.text #return the text
except Exception as er:
print(er)
return "ERROR"`
It often throws a StaleReferenceException
on the send_keys(term)
, which confused me as the lines before it that verify the element is present do not throw any error. I've tried removing some portion of the wait and tried adding more, but the result is the same.
答案1
得分: 1
"StaleReferenceException" 是在元素在被找到后和下一次尝试与之交互或读取值之间发生变化时出现的异常。我建议始终在与元素交互之前立即查找该元素。在这种情况下,可以这样做:
driver.find_element(By.ID, "search-box").send_keys(term)
英文:
A StaleReferenceException
happens when the element changes between the time it was found, and the next time you try to interact with, or read a value from, it. I always recommend finding the element right before interacting with it. In this situation, that would be
driver.find_element(By.ID, "search-box").send_keys(term)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论