Locating an element using Selenium with Python

advertisements

Edited based on the answers:

I am using Selenium with Python and trying to locate a button on an web application on Chrome. The block of code has an iframe as mentioned in the answer.

<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

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. Just a list of items in the application expands. Also, IDs and the Names are unique for the seven five menu buttons. 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.


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.

https://github.com/sdl/ISHRemote

For example, if you're looking for all the directories under '\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.

Hope that helps.