代码之家  ›  专栏  ›  技术社区  ›  lord stock

硒在iframe中引发与下拉菜单相互作用的非接触元素异常

  •  1
  • lord stock  · 技术社区  · 3 年前

    我正在做以下工作 link

    我甚至无法提取第一个下拉列表。 我一直在使用selenium 3.14版

    我编写了以下代码:

    user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, " \
                 "like Gecko) Chrome/100.0.4896.60 Safari/537.36"
    options = webdriver.FirefoxOptions()
    options.headless = False
    options.add_argument(f'user-agent={user_agent}')
    options.add_argument("--window-size=1920,1080")
    options.add_argument('--ignore-certificate-errors')
    options.add_argument('--allow-running-insecure-content')
    options.add_argument("--disable-extensions")
    options.add_argument("--proxy-server='direct://'")
    options.add_argument("--proxy-bypass-list=*")
    options.add_argument("--start-maximized")
    options.add_argument('--disable-gpu')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument('--no-sandbox')
    driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(), options=options)
    driver.get("https://election.gov.np/np/page/voter-list-db")
    driver.implicitly_wait(5)
    
    Select(WebDriverWait(driver, 5).until(EC.presence_of_element_located(
                    (By.XPATH, "//select[@id='state']")))).select_by_value("5")
    time.sleep(5)
    

    我总是得到:

    selenium.common.exceptions.TimeoutException: Message: 
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   undetected Selenium    3 年前

    文本为 电子邮件登录 在一个小时之内 <iframe> 所以你必须:

    • 诱导 WebDriverWait 为了想要的 框架可用并切换到它 .

    • 诱导 WebDriverWait 为了想要的 元素是可点击的 .

    • 您可以使用以下任一选项 Locator Strategies :

      • 使用 XPATH :

        driver.get("https://election.gov.np/np/page/voter-list-db")
        WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@name='bbvrs']")))
        Select(WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='state']")))).select_by_value("5")
        
    • 笔记 :您必须添加以下导入:

       from selenium.webdriver.support.ui import WebDriverWait
       from selenium.webdriver.common.by import By
       from selenium.webdriver.support import expected_conditions as EC
      
    • 浏览器快照:

    EC_Nepal


    参考

    你可以在以下网站上找到一些相关的讨论: