【问题标题】:Not able to click button with Selenium (Python)无法使用 Selenium(Python)单击按钮
【发布时间】:2021-09-11 13:14:33
【问题描述】:

我正在尝试使用 Selenium 单击一个按钮,但每次我收到它无法找到该元素的消息。即使我在它前面放了一个 time.sleep() 也会发生这种情况。

time.sleep(5)

                #Click on download
                downloadButton = driver.find_element_by_css_selector('#downloadButton')
                time.sleep(5)
                downloadButton.click()

                #Click on close
                closeButton = driver.find_element_by_xpath('//*[@id="btnZorgAnnuleren"]')
                closeButton.click()

按钮在框架内(不是 iframe),但我不知道如何切换到它。这里是框架源代码和页面源代码:https://www.codepile.net/pile/ZoG0REzQ。谁能帮帮我?

【问题讨论】:

  • 我没有看到任何 iframe 或 frame 标签。也不是downloadButton 也不是btnZorgAnnuleren
  • 我添加了一个新链接。这对你有用吗@cruisepandey?当我右键单击弹出窗口时,我可以选择查看帧源。这意味着有一个框架对吗?我是这么认为的。
  • 我现在可以看到内容,但是看不到`downloadButton orbtnZorgAnnuleren`,你可以试试同样的方法并使用CTRL + F搜索看看它是否在你的最后工作
  • FCan you do this.. 打开 chrome -> 转到开发工具(按 F12)然后 -> 元素 -> 然后执行 CTRL + F 并键入 //frame 或 //iframe..您会在控制台中看到匹配项
  • 你是对的!当我检查元素时,它确实出现了,但是当我查看页面源时,它没有。我刚刚在检查元素代码中看到了 iframe ID。我的坏,我要试试这个。我会及时向大家发布。谢谢!

标签: python selenium frame


【解决方案1】:

如果是 iframe :

我建议你切换到这样的框架(显式等待):

wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iframe ID")))

进口:

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

切换后,您可以像这样点击:

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#downloadButton"))).click()

然后点击关闭:

 wait.until(EC.element_to_be_clickable((By.ID, "btnZorgAnnuleren"))).click()

一旦你完成了,你应该像这样切换到默认内容:

driver.switch_to.default_content()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多