【问题标题】:Selecting item in nested html Frame with Selenium WebDriver使用 Selenium WebDriver 在嵌套的 html 框架中选择项目
【发布时间】:2018-02-20 22:44:11
【问题描述】:

我正在尝试使用 Selenium WebDriver 在网页上选择名为 cslLogin 的链接。它位于一个名为topframe 的子框架中,但我无法访问它,即使在我切换到它的父框架TopLevelFrame 之后我也可以成功完成。 html 页面具有以下基本布局:

<html>
 <head>...</head>
  <frameset name="ATopLevelFrameSet">
   <frame name="TopLevelFrame">
    #document
     <html>
      <head></head>
       <frameset name="Aframeset">
        <frame name="topframe">
         #document
          <html>
           <head>...</head>
           <body class="clsBgColor">
            <table id="tblTitle">
             <tbody>
              <tr class="clsBackYellow"">
               <td class="clsDeviceStatusLink">
                <a class="clsLogin" href="javascript:void(0);"
                onclick="javascript:fnnLoginClick();">Login</a> == $0
               etc...

我可以使用self.driver.switch_to.frame("TopLevelFrame") 成功切换到TopLevelFrame,但我无法访问topframeclsLogin(我分别得到NoSuchFrameExceptionNoSuchElementException

我试过find_element_by_namefind_element_by_xpathfind_element_by_link_textfind_element_by_css_selector,也用过

try:
    element = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.NAME, "TopLevelFrame"))
            )
finally:
    self.driver.quit()

如果这是时间/页面加载问题,但在页面加载后很长时间超时。

我从其他帖子中知道,我需要先切换到最近的框架,然后才能访问该元素,但这当然行不通。有什么建议么?提前致谢。

【问题讨论】:

标签: python selenium webdriver nosuchelementexception


【解决方案1】:

要点击链接 cslLogin 首先你必须切换到 TopLevelFrame &lt;frame&gt; 然后到 topframe &lt;frame&gt; 然后点击链接如下:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.NAME,"TopLevelFrame"))
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.NAME,"topframe"))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Login"))).click()
# Or
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//tr[@class='clsBackYellow']/td[@class='clsDeviceStatusLink']/a[@class='clsLogin']"))).click()

【讨论】:

  • 谢谢!我曾尝试等待几个帧/元素,但事实证明我需要等待所有内容。
【解决方案2】:

你应该使用“find_element_by_xpath” 只需打开网站,按“ctrl+shift+c”并检查您要提取的内容。 在元素中找到行,右键复制xpath,粘贴到函数中。

例如,考虑这个页面来源:

<html>
 <body>
  <form id="loginForm">
   <input name="username" type="text" />
   <input name="password" type="password" />
   <input name="continue" type="submit" value="Login" />
   <input name="continue" type="button" value="Clear" />
  </form>
</body>
<html>

表单元素可以这样定位:

login_form = driver.find_element_by_xpath("/html/body/form[1]")
login_form = driver.find_element_by_xpath("//form[1]")
login_form = driver.find_element_by_xpath("//form[@id='loginForm']")

欲了解更多信息,请查看http://selenium-python.readthedocs.io/locating-elements.html

【讨论】:

  • 它似乎仍然无法正常工作。这是我上次运行的代码示例:top_level_frame = self.driver.find_element_by_xpath('/html/frameset/frame') self.driver.switch_to.frame(top_level_frame) top_frame = self.driver.find_element_by_xpath('/html/frameset/frame[1]') self.driver.switch_to.frame(top_frame) element = self.driver.find_element_by_xpath('//*[@id="tblTitle"]/tbody/tr[1]/td[3]/a') 在找到topframe 时,我仍然收到NoSuchElementFound 异常。它切换到TopLevelFrame 非常好。
【解决方案3】:

试试这个,因为你有嵌套的 iframe,切换到 TopLevelFrame,然后切换到 topframe 并执行你的 find_element_* 调用

driver.switch_to.frame(driver.find_element_by_name('TopLevelFrame'))
driver.switch_to.frame(driver.find_element_by_name('topframe'))
driver.find_element_by_class_name('clsLogin').click()

让我知道这是否有效

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 2023-03-07
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    相关资源
    最近更新 更多