使用Selenium和Python

时间:2017-03-23 06:58:29

标签: python selenium

根据答案编辑:

我正在使用Selenium和Python,并试图在Chrome上的Web应用程序上找到一个按钮。代码块有一个iframe,如答案中所述。

<iframe data-bind="attr: { src: src, foo: $root.registerTargetDisplayFrame($data, $element) }, event: {load: function() {loaded(true);}, focus: $root.blurredNavigationPane}" src="https://products.com/InfoShareAuthor/home">
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
      <html>
        <head>code here
        <frameset id="IshTop" class="infoshareauthor" framespacing="0" border="0" bordercolor="#FFFFFF" frameborder="0" rows="31,25,*,0">
           <frame id="MenuBar" scrolling="no" name="MenuBar" src="./MainMenuBar.asp">
             <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
               <html>
                 <head>
                 <body>
                   <div id="Top-Menu-Container">
                      <div id="top-menu-wrapper">
                         <div id="top-menu">
                            <form name="MainBar">
                               <script type="text/javascript" language="javascript">
                               <table cellspacing="0" cellpadding="0" border="0">
                                 <tbody>
                                    <tr>
                                       <td width="95" valign="bottom">
                                       <td width="95" valign="bottom">
                                          <div style="POSITION: relative;">
                                              <div height="30" style="POSITION: absolute; z-index:0; top: 4px; margin-left: -5px">
                                                 <a href="javascript:TabSelect(1);">
                                                    <img border="0" src="./UIFramework/tab_active.png">
                                                 </a>
                                              </div>
                                              <div onclick="javascript:TabSelect(1);" style="POSITION: absolute; z-index:2; top: -8px">
                                                 <table cellspacing="0" cellpadding="0" border="0">
                                                     <tbody>
                                                         <tr>
                                                            <td id="MenuButton1" class="tab_active" width="95" valign="bottom" height="30" align="center" style="cursor:pointer;padding-bottom:2px;" name="Repository">Repository</td>
                                                         </tr>
                                                     </tbody>
                                                 </table>
                                              </div>
                                            </div>
                                          </td>
                                          <td width="95" valign="bottom">
                                          <td width="95" valign="bottom">
                                          <td width="95" valign="bottom">
                                          <td width="95" valign="bottom">
                                        </tr>
                                    </tbody>
                                 </table>
                               </form>
                            </div>
                            <div id="top-help">
                            <div id="top-nav-links">
                          </div>
                        </div>
                   </body>
             </html>
       </frame>
         <frame id="BreadCrumbs" frameborder="0" border="0" scrolling="no" name="BreadCrumbs" src="./BreadCrumbs.asp">
         <frameset id="Application" bordercolor="#0099CC" frameborder="0" rows="0,*,0,0,0,0">
         <frameset id="HiddenFrameSet" bordercolor="#0099CC" frameborder="0" rows="0,0,,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1">
      <noframes> It looks like your browser doesn't support frames. This page requires frames in order to function. <br><br>For more information, please <a href='http://www.trisoftcms.com/en/contact-us.html' target=_blank style='white-space:nowrap'>contact us</a>. </noframes>
   </frameset>
  </html>
</iframe>

我用这个换帧:

iframe = browser.find_element_by_xpath("//iframe[@src='https://products.com/InfoShareAuthor/home']")
browser.switch_to.frame(iframe)

我写的代码:

browser.find_element_by_xpath("//td[@id='MenuButton1'][@name='Repository'][contains(text(),'Repository')]")

当我进行Firebug搜索时,我可以使用此xpath找到该元素

我也尝试过:

browser.find_element_by_id("MenuButton1")

browser.find_element_by_name("Repository")

注意:单击按钮时,URL不会更改。只是应用程序中的项目列表扩展。此外,ID和名称对于七个五个菜单按钮是唯一的。没有任何菜单按钮工作。

有没有人知道可能出错的地方?我是Python和Selenium的新手。

2 个答案:

答案 0 :(得分:0)

您可以尝试加载iframe网址。它避免了从iframe等待加载

的selenium问题

答案 1 :(得分:0)

这并没有完全回答你的问题,但它确实解决了你想要做的事情:很可能你可以用SDL的API客户端ISHRemote完成同样的任务(以及许多其他任务)。

https://github.com/sdl/ISHRemote

例如,如果您要查找'\ General'下的所有目录:

Import-Module ISHRemote
# first authenticate
$session = New-IshSession -IshPassword $password -IshUserName $username -WsBaseUrl 'https://ccms.example.com/InfoShareWS/'
# get a list of all the child folders under General
Get-IshFolder -IshSession $session -FolderPath '\General' -Recurse -Depth 2

或者,如果您正在尝试获取特定目录中的文件列表:

Import-Module ISHRemote    
# first authenticate
$session = New-IshSession -IshPassword $password -IshUserName $username -WsBaseUrl 'https://ccms.example.com/InfoShareWS/'
# get all content in this folder
Get-IshFolderContent -IshSession $session -FolderPath 'General\path\to\topics'

使用ISHRemote,您还可以查找和更新出版物,移动内容,修改元数据等。

希望有所帮助。

相关问题