Home > front end >  Click the correct edit link next to certain item using Python Selenium
Click the correct edit link next to certain item using Python Selenium

Time:01-02

This is my first ever post.

I have recently started using Python and Selenium to do some administrative work on several websites. All websites have the same domain and are setup in a similar way.

After searching online for about a week now I wasn't able to find anything related to the following issue. I will try to explain the problem I'm facing:

website 1:

Apple   Remove  Edit Copy
Banana  Remove  Edit Copy
Pear    Remove  Edit Copy

        <tbody><tr>
            <td></td> 
            <td align="left"><span id="Items_01_ItemName">Apple</span></td> 
            <td><a id="Items_01_Remove" href="hidden">Remove</a>                
            <a id="Items_01_Edit" href="hidden">Edit</a>
            <a id="Items_01_Copy" href="hidden">Copy</a></td>
        </tr>
    
        <tr>
            <td></td> 
            <td align="left"><span id="Items_02_ItemName">Banana</span></td> 
            <td><a id="Items_02_Remove" href="hidden">Remove</a>                
            <a id="Items_02_Edit" href="hidden">Edit</a>
            <a id="Items_02_Copy" href="hidden">Copy</a></td>
        </tr>
    
        <tr>
            <td></td> 
            <td align="left"><span id="Items_03_ItemName">Pear</span></td> 
            <td><a id="Items_03_Remove" href="hidden">Remove</a>                
            <a id="Items_03_Edit" href="hidden">Edit</a>
            <a id="Items_03_Copy" href="hidden">Copy</a></td>
        </tr>

website 2:

Banana  Remove  Edit Copy
Pear    Remove  Edit Copy
Apple   Remove  Edit Copy

        <tbody><tr>
            <td></td> 
            <td align="left"><span id="Items_01_ItemName">Banana</span></td> 
            <td><a id="Items_01_Remove" href="hidden">Remove</a>                
            <a id="Items_01_Edit" href="hidden">Edit</a>
            <a id="Items_01_Copy" href="hidden">Copy</a></td>
        </tr>
    
        <tr>
            <td></td> 
            <td align="left"><span id="Items_02_ItemName">Pear</span></td> 
            <td><a id="Items_02_Remove" href="hidden">Remove</a>                
            <a id="Items_02_Edit" href="hidden">Edit</a>
            <a id="Items_02_Copy" href="hidden">Copy</a></td>
        </tr>
    
        <tr>
            <td></td> 
            <td align="left"><span id="Items_03_ItemName">Apple</span></td> 
            <td><a id="Items_03_Remove" href="hidden">Remove</a>                
            <a id="Items_03_Edit" href="hidden">Edit</a>
            <a id="Items_03_Copy" href="hidden">Copy</a></td>
        </tr>

How can I make selenium click the edit link for instance for the item Pear? For website 1 it is the edit link no. 3 and for website 2 it is edit link no.2

I would like my script to be able to click the edit link for the item "Pear" regardless of its position.

I hope my question is clear and someone is able to point me in the correct direction.

Thank you.

CodePudding user response:

you can filter for name ->

if driver.find_element_by_xpath('//*[@id="DwsldCn"]/div/table/tbody/tr[1]/td[4]/a').get_attribute("name")=="banana": print("perfect"

CodePudding user response:

Try this:

driver.find_element_by_xpath("//span[contains(@id, 'ItemName')][text()='Pear']/following::a[contains(@id, 'lnkEdit')]")

  • Related