1

I am trying to gather some information from a website, using selenium. I am interested in some information (img) within div element:

<div class="entry-content clearfix"> 
 ...
  <img data-attachment-id="7677" data-permalink="https://test_site.com/leftcentre/" ... alt="Example of site" >
  <img data-attachment-id="98231" data-permalink="https://test_site.com/high/" ... alt="another site" >

The values of the img data-attachment-id may change: so I could have 7677, 7664 and other values. This means that I could have the following Xpaths among many many others:

  •     //*[@id="post-63779"]/div/h2[1]/img[1]
    
  •     //*[@id="post-781"]/div/header/h1/a/img
    

What I have done so far to extract this information is shown below:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
        
        driver=webdriver.Chrome('my_path')            
        response=driver.get('https://website)
                
        wait = WebDriverWait(driver, 20)
        
        x = driver.find_element_by_xpath('//*[@id="post-781"]/div/header/h1/a/img').text

        # print(x)
        
        return x

but probably I am making some mistakes since I have no outputs and chrome still continues to look for the element. I am wondering if there might be a chance to get the image without explicitly referring to the post number or elements in between div and img, or just to extract all img data-attachment-id information. In case my question or the path is not clear, please let me know and I will provide you with more info.

Math
  • 191
  • 2
  • 5
  • 19

1 Answers1

0

The value of the attribute data-attachment-id is being generated dynamically. So on each access to the AUT (Application Under Test) the value will keep on changing.

Hence it won't be possible to locate the same element with a predefined static locator strategy.

In these cases, the solution is to use dynamic locator strategies.


References

You can find a couple of relevant detailed discussions on dynamic locators in:


Update

Beside each number within the data-attachment-id attribute value, there should be an unique value for either of an attribute which which you would have to use to identify the element uniquely. Else you can also use an index. As an example, to extract the text Example of site, you can use the following Locator Strategies:

  • Using css_selector:

    print(driver.find_element_by_css_selector("div.entry-content img[data-attachment-id][data-permalink*='leftcentre']").get_attribute("alt"))
    
  • Using xpath:

    print(driver.find_element_by_xpath("//div[@class='entry-content clearfix']//img[@data-attachment-id and contains(@data-permalink, 'leftcentre')]").get_attribute("alt"))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you so much for your help and the references, DebanjanB. From what I have understood, in my case I would need probably prefer starts with or contains to include post: `//*[starts-with(@id, coption]/div/h2[1]/img[1]`. However, it is not not clear to me how to include also information on div and img using contain – Math Nov 02 '21 at 12:38
  • Checkout the answer update and let me know the status. – undetected Selenium Nov 02 '21 at 13:39