【问题标题】:Selenium: different behaviour debugging versus running. Wrong WebDriverWait?Selenium:调试与运行的不同行为。错误的 WebDriverWait?
【发布时间】:2022-01-25 13:12:15
【问题描述】:

如果手动进入调试,以下代码会按预期运行,而如果只是运行,则会出现异常。

除了一些到达搜索页面的初始代码之外,我还请求一个我知道不存在的值(“ZQZZQ”),因此代码应该分支到最后一个 else 语句。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(
    "/Users/bob/Documents/work/AIFA/scraper/scrape_gu/chromedriver"
)

# navigate through the initial agreement screens
driver.get("https://farmaci.agenziafarmaco.gov.it/bancadatifarmaci/cerca-farmaco")
readunderstood = driver.find_element_by_id("conf")
readunderstood.click()
accept = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "/html/body/div[5]/div[3]/div/button"))
)
accept.click()
# end of the initial agreement screens and general preparation
##############################################################

string_to_search = "ZQZZQ"  # we can safely assume this does not exist

find_textbox = driver.find_element_by_id("search")
find_textbox.clear()  # after the first search the old value will still be there
find_textbox.send_keys(string_to_search)
find_textbox.send_keys(Keys.ENTER)
# check if any results found
element = WebDriverWait(driver, 5).until(
    EC.presence_of_element_located((By.ID, "noresultsfound"))
)
# if we did retrieve something we don't have an error message
if element.text != "Nessun risultato trovato":
    # first we have to find out the total number of retrieved pages
    print(f"Digram {string_to_search} found!")
else:
    print(f"Digram {string_to_search} not found. Need to close modal window!")
    # click on ok of name not found modal dialog
    WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.XPATH, "/html/body/div[4]/div[3]/div/button"))
    ).click()
    print("Modal window hopefully closed")

当我全速运行它时,我发现它进入“if”分支,打印“Digram ZQZZQ found!”这是错误的。

WebDriverWait 调用是否错误?谢谢

【问题讨论】:

    标签: python selenium web-scraping


    【解决方案1】:

    您需要将“presence_of_element_located”方法更改为“visibility_of_element_located”。

    旧代码:

    # check if any results found
    element = WebDriverWait(driver, 5).until(
        EC.presence_of_element_located((By.ID, "noresultsfound"))
    

    更改:

    element = WebDriverWait(driver, 5).until(
        EC.visibility_of_element_located((By.XPATH, "//*[@id='noresultsfound']"))
    )
    

    输出

    Digram ZQZZQ not found. Need to close modal window!
    Modal window hopefully closed
    

    VisibilityOfElementLocated 与 PresenceOfElementLocated:

    https://stackoverflow.com/questions/38038920/visibilityofelementlocated-vs-presenceofelementlocated#:~:text=while%20the%20visibilityOfElementLocated%20has%20to,of%20presenceOfElementLocated%20will%20be%20enough.

    【讨论】:

    • 非常感谢。工作,也在学习新事物:)
    猜你喜欢
    • 1970-01-01
    • 2015-01-28
    • 2017-09-27
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多