0

I have a website that has anywhere from 3 to 4 images on it. If it tries to acquire the 4th image and it's not there the program blows up. I want to check if the fourth is there, and if it is I want to acquire it, if not I want to go on with the program. I want to do it without using try and except. I've tried .isDisplayed could not get it to work.

This is the driver statement that acquires the image. Image3 = driver.find_element_by_xpath('//*[@id="goods_thumb_content"]/ul/li[3]').get_attribute("data-bigimg");

I'm using Selenium and here is the code:

      #This is the code when all four images are present. and when it's not the last <li> <l/> is not 
      present.

     <ul>
     <li class="thumb_item active logsss_event_cl" data-isself="1" data- 
     bigimg="https://gloimg.rglcdn.com/rosegal/pdm-product-pic/Clothing/2021/03/09/source- 
     img/20210309165545_60473811b6a7c.jpg" data-zoomimg="https://gloimg.rglcdn.com/rosegal/pdm- 
     product-pic/Clothing/2021/03/09/source-img/20210309165545_60473811b6a7c.jpg" data-logsss-const- 
     value="{'x': 'change_pic'}" style="height: 131.6px;">  <img 
     src="https://gloimg.rglcdn.com/rosegal/pdm-product-pic/Clothing/2021/03/09/grid- 
     img/1618874299184494137.jpg?im_scale=w75_1x" alt="Plus Size Paisley Print Empire Waist 
     Asymmetric Tank Top -  "> </li>

     <li class="thumb_item logsss_event_cl" data-isself="1" data- 
     bigimg="https://gloimg.rglcdn.com/rosegal/pdm-product-pic/Clothing/2021/03/09/source- 
     img/20210309165545_60473811c9ce8.jpg" data-zoomimg="https://gloimg.rglcdn.com/rosegal/pdm- 
     product-pic/Clothing/2021/03/09/source-img/20210309165545_60473811c9ce8.jpg" data-logsss-const- 
     value="{'x': 'change_pic'}" style="height: 131.6px;">  <img 
     src="https://gloimg.rglcdn.com/rosegal/pdm-product-pic/Clothing/2021/03/09thumb- 
     img/1615251725078779977.jpg?im_scale=w75_1x" alt="Plus Size Paisley Print Empire Waist 
     Asymmetric Tank Top -  "> </li>

     <li class="thumb_item logsss_event_cl" data-isself="1" data- 
     bigimg="https://gloimg.rglcdn.com/rosegal/pdm-product-pic/Clothing/2021/03/09/source- 
     img/20210309165545_60473811df353.jpg" data-zoomimg="https://gloimg.rglcdn.com/rosegal/pdm- 
     product-pic/Clothing/2021/03/09/source-img/20210309165545_60473811df353.jpg" data-logsss-const- 
     value="{'x': 'change_pic'}" style="height: 131.6px;">  <img 
     src="https://gloimg.rglcdn.com/rosegal/pdm-product-pic/Clothing/2021/03/09thumb- 
     img/1615251725121900529.jpg?im_scale=w75_1x" alt="Plus Size Paisley Print Empire Waist 
     Asymmetric Tank Top -  "> </li>           

     #The <li> below is not present when the fourth image is not there.

    <li class="thumb_item logsss_event_cl" data-isself="1" data- 
    bigimg="https://gloimg.rglcdn.com/rosegal/pdm-product-pic/Clothing/2021/03/09/source- 
    img/20210309165545_60473811f1542.jpg" data-zoomimg="https://gloimg.rglcdn.com/rosegal/pdm- 
    product-pic/Clothing/2021/03/09/source-img/20210309165545_60473811f1542.jpg" data-logsss-const- 
    value="{'x': 'change_pic'}" style="height: 131.6px;">  <img 
    src="https://gloimg.rglcdn.com/rosegal/pdm-product-pic/Clothing/2021/03/09thumb- 
    img/1615251725802705831.jpg?im_scale=w75_1x" alt="Plus Size Paisley Print Empire Waist Asymmetric 
    Tank Top -  "> </li> 
    </ul>
tejas
  • 1
  • 2
  • Does this help? https://stackoverflow.com/q/2646195/13552470 – Red May 31 '21 at 13:25
  • 'If it tries to acquire the 4th image and it's not there the program blows up.' -- what does "blows up" mean? Do you get an exception? – C. Peck May 31 '21 at 13:31
  • How would I find the len of the ul or the number of li's in the code. Then I could just do a for in range. – tejas May 31 '21 at 16:14

1 Answers1

0

Instead of identifying each element individually like this

Image3 = driver.find_element_by_xpath('//*[@id="goods_thumb_content"]/ul/li[3]').get_attribute("data-bigimg")

Make use of find_elements, maybe something like this:

elements = driver.find_elements_by_xpath('//*[@id="goods_thumb_content"]/ul/li')
images = []
for element in elements:
    images.append(element.get_attribute("data-bigimg"))

This will give you an array with however many elements there are matching '//*[@id="goods_thumb_content"]/ul/li' To define your Image1, Image2, etc. you can get them from images

Image1 = images[0]
Image2 = images[1]
....
C. Peck
  • 3,641
  • 3
  • 19
  • 36
  • It get and error: File "C:\Users\daver\Dev\rosegal\rosegal\this.py", line 112 images.append(element.get_attribute("data-bigimg"))") ^ SyntaxError: EOL while scanning string literal – tejas May 31 '21 at 14:21
  • You have a typo, there is an extra `")` at the end of your line: `("data-bigimg"))")` should be `("data-bigimg"))` – C. Peck May 31 '21 at 14:24
  • Is this the array '//*[@id="goods_thumb_content"]/ul/li' jhow do I Make Image1 = and Image2 = . . . – tejas May 31 '21 at 14:53
  • If that's what you really want just define `Image1 = images[0]`, see updated answer – C. Peck May 31 '21 at 14:56
  • Thanks for trying to help me but Now I'm get Image4 = images[4] IndexError: list index out of range – tejas May 31 '21 at 15:01
  • It should be `Image4 = images[3]` – C. Peck May 31 '21 at 15:02