簡體   English   中英

在Python中使用Selenium定位元素

[英]Locating an element using Selenium with Python

根據答案進行編輯:

我將Selenium與Python結合使用,並嘗試在Chrome的網絡應用程序上找到按鈕。 代碼塊具有答案中提到的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非常陌生。

您可以嘗試加載iframe網址。 它避免了硒從iframe等待加載的問題

這不能完全回答您的問題,但是可以解決您要嘗試做的事情:很可能您可以使用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,您還可以查找和更新出版物,移動內容,修改元數據等。

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM