【问题标题】:Can't Access Dropdown In Iframe Python Selenium无法访问 Iframe Python Selenium 中的下拉菜单
【发布时间】:2021-03-26 19:23:17
【问题描述】:

感谢您查看我的问题,感谢您的帮助。

我查看了可用的答案,但似乎没有一个对我有用。其他答案似乎涉及切换到 iframe,我成功地做到了(否则它会更快崩溃,对吗?),然后我无法在该 iframe 中选择下拉菜单。

我尝试的一些方法:

Unable to click on dropdown within iframe - Selenium Python

Can't access dropdown select using Selenium in Python

Python Selenium - Cant Click on Button (iFrame Solved !)

由于某种原因,该元素不存在 - 我尝试使用等待语句(sleep 和 EC.waits),但这也不起作用。当我观察程序运行时,可以看到驱动成功加载了 iframe 并且元素是可选择的,所以我认为这可能是您对 iframe 元素的访问方式的误解。

这是代码,您会注意到它位于登录块后面,我不知道在这种情况下我能做些什么来帮助人们达到这一点,但我已经分享了所有我能做到的 html 和代码考虑并可以根据要求分享更多内容:

def clear_wardrobe():
    """Clears the user's wardrobe so that they have no items listed"""
    try:
        # Initialize
        log_in()

        # Fetch the number of items on the page and loop through all of them.
        while True:
            driver.get("https://www.grailed.com/users/myitems")
            time.sleep(2)
            item = driver.find_element_by_xpath("//*[@id=\"wardrobe\"]/div/div[3]/div/div[2]/div[2]/div/div[1]/div[1]/a/div[2]/img")
            item.click()
            time.sleep(3)

            driver.switch_to.window(driver.window_handles[1])

            element = driver.find_element_by_xpath("/html/body/div[8]/div/div[2]/div[3]/div[9]/div[2]/span/a")
            actions = ActionChains(driver)
            actions.move_to_element(element).perform()

            time.sleep(0.5)
            element = driver.find_element_by_xpath("//*[@id=\"Listing--Actions\"]/div/div[1]")
            element.click()
            time.sleep(1)

            # switch to specific iframe and select "Sold Elsewhere option" - ISSUE IS HERE
            frame = driver.find_element_by_xpath("//iframe[contains(@title,'Optimizely Internal Frame')]")
            driver.switch_to.frame(frame)
            select_box = Select(driver.find_element_by_name("reason"))
            select_box.select_by_visible_text("Sold Elsewhere")
            time.sleep(3)
            driver.find_element_by_xpath("//*[@type='submit']").click()

    except NoSuchElementException:
        print("Something went wrong in clearWardrobe()")
        traceback.print_exc()

这是带有下拉菜单的页面 - 我想选择在其他地方出售

这是错误信息:

Something went wrong in clearWardrobe()
Traceback (most recent call last):
  File "/Users/thomas.mclaughlin/Documents/GitHub/WardrobeWizard2/scripts/core.py", line 110, in clear_wardrobe
    select_box = Select(driver.find_element_by_name("reason"))
  File "/Users/thomas.mclaughlin/Library/Python/3.8/lib/python/site-packages/selenium/webdriver/remote/webdriver.py", line 496, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "/Users/thomas.mclaughlin/Library/Python/3.8/lib/python/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/Users/thomas.mclaughlin/Library/Python/3.8/lib/python/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Users/thomas.mclaughlin/Library/Python/3.8/lib/python/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="reason"]"}
  (Session info: chrome=89.0.4389.90)

【问题讨论】:

  • 它似乎不在 iframe 中。
  • 那里只是一个模态div...不需要切换到frame。
  • @pcalkins 我开始这些恶作剧是因为我无法选择模式上的元素 - 这让我认为这是一个 iframe 问题,我将尝试重新创建第一个问题并更新这篇文章。
  • @pcalkins 哇,我真的很尴尬,很抱歉 - 我不敢相信我没有这样做......

标签: python selenium selenium-chromedriver


【解决方案1】:

原来元素是模态的,而不是 iframe,所以这个问题是有缺陷的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 2017-11-08
    相关资源
    最近更新 更多