简体   繁体   English

在Python中使用Selenium定位元素

[英]Locating an element using Selenium with Python

Edited based on the answers: 根据答案进行编辑:

I am using Selenium with Python and trying to locate a button on an web application on Chrome. 我将Selenium与Python结合使用,并尝试在Chrome的网络应用程序上找到按钮。 The block of code has an iframe as mentioned in the answer. 代码块具有答案中提到的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>

I switched frames using this: 我使用以下方法切换帧:

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

The code that I wrote: 我写的代码:

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

I could find the element using this xpath when I did a Firebug search 当我执行Firebug搜索时,可以使用此xpath找到元素

I also tried: 我也尝试过:

browser.find_element_by_id("MenuButton1")

and

browser.find_element_by_name("Repository")

Note: When I click the button, the URL does not change. 注意:当我单击按钮时,URL不会更改。 Just a list of items in the application expands. 只会扩展应用程序中的项目列表。 Also, IDs and the Names are unique for the seven five menu buttons. 此外,七个五个菜单按钮的ID和名称是唯一的。 None of the menu buttons work. 菜单按钮均无效。

Does any one have any idea about what might be wrong? 是否有人对可能出什么问题有任何想法? I am very new to Python and Selenium. 我对Python和Selenium非常陌生。

you can try load iframe url. 您可以尝试加载iframe网址。 It avoids issues with selenium waiting from the iframe to load 它避免了硒从iframe等待加载的问题

This doesn't exactly answer your question, but it does address what you're trying to do: it is likely you can accomplish the same task (and many others) with SDL's API client ISHRemote. 这不能完全回答您的问题,但是可以解决您要尝试做的事情:很可能您可以使用SDL的API客户端ISHRemote完成相同的任务(以及许多其他任务)。

https://github.com/sdl/ISHRemote https://github.com/sdl/ISHRemote

For example, if you're looking for all the directories under '\\General': 例如,如果要查找“ \\ 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

Or if you're trying to get a list of files in a particular directory: 或者,如果您尝试获取特定目录中的文件列表:

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'

With ISHRemote, you can also find and update publications, move content, modify metadata, etc. 使用ISHRemote,您还可以查找和更新出版物,移动内容,修改元数据等。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM