【问题标题】:Selenium- Click a button issueSelenium-单击按钮问题
【发布时间】:2021-06-29 19:27:37
【问题描述】:

使用导入:

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

使用此 def 导航到按钮所在的确切 iFrame:

def changeIframetoManagement():
    wait2 = WebDriverWait(driver, 10)
    wait2.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "moduleManagement")))

我尝试完成这项任务:

changeIframetoManagement()
driver.find_element_by_css_selector("body > div.bootbox.modal.fade.show-order-info-bootbox- 
container.in > div > div > div.modal-body > button").click()

我也尝试像这样通过 xPath 到达

driver.find_element_by_xpath("//html/body/div[5]/div/div/div[1]/button").click()

那是我在的最后一帧

def changeIframetoOrders():
   wait3 = WebDriverWait(driver, 10)
   wait3.until(EC.frame_to_be_available_and_switch_to_it((By.NAME, 
   "orderInfo")))

【问题讨论】:

    标签: python-3.x selenium webdriver


    【解决方案1】:

    切换到 iframe 后试试这个:

    driver.find_element_by_css_selector("div.modal-dialog button.bootbox-close-button").click()
    

    UPD
    根据您上次的 cmets,由于您之前切换到位于 orderInfo 名称的 iframe,因此在切换到位于 moduleManagement id 的 iframe 之前,您必须切换到默认内容,如下所示:

    changeIframetoOrders()
    driver.switch_to.default_content()
    changeIframetoManagement()
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.modal-dialog button.bootbox-close-button"))).click()
    

    【讨论】:

    • 得到了回溯,正如我在上面的答案中提到的那样
    • 所以你的问题不在于按钮。问题在于切换到 iframe
    • 我编辑了这个问题,所以你可以看到我所在的最后一个 iFrame,也许你可以在那里看到,总而言之,我的按钮在 moduleManagement iframe 中,而不是在 Orderinfo 中,我使用上面的 Def 来之间切换。也许我在那里穿了一些东西,无论如何非常感谢你
    • 那么,您在 orderInfo iframe 中,并试图直接从那里切换到 modalManagement iframe,对吗?如果是这样,您首先需要从第一个 iframe 切换到默认内容,然后再切换到第二个 iframe!
    • 顺便说一句,您不需要为每个方法定义单独的等待实例。您可以改为定义一个全局等待。
    【解决方案2】:

    看看这个 xpath 是否有效:-

    driver.find_element_by_xpath(".//button[contains(@class,'close-button')]").click()
    

    【讨论】:

    • 回溯(最近一次调用最后一次): changeIframetoManagement() changeIframetoManagement wait2.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "moduleManagement"))) 文件 "C:\Users\Benco \PycharmProjects\pythonProject\venv\lib\site-packages\selenium\webdriver\support\wait.py",第 80 行,直到引发 TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Process finished退出代码 1
    • 顺便说一句,我必须注意到,在我再次切换到那个 iframe 之前,我在另一个 iframe 上,也许转换有问题?
    【解决方案3】:

    x 按钮位于 moduleManagement iframe 中。

    目标是使用 Selenium 点击它。

    根据 OP

    wait3.until(EC.frame_to_be_available_and_switch_to_it((By.NAME, 
       "orderInfo")))
    

    看起来他在不同的 iframe orderInfo

    因此,我建议您在完成任何 iframe 内容后,始终将控件切换回默认内容,如下所示:

    driver.switch_to.default_content()
    

    现在你必须再次切换到moduleManagement iframe,如下所示:

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

    然后像这样点击 x 按钮:

    wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, ''bootbox-close-button) and text()='x']"))).click()
    

    【讨论】:

      猜你喜欢
      • 2021-03-29
      • 2019-02-07
      • 2013-12-24
      • 2019-05-28
      • 1970-01-01
      • 2013-05-07
      • 2021-05-04
      • 2021-03-18
      • 1970-01-01
      相关资源
      最近更新 更多