Home > Enterprise >  Select HTML elements with Selenium in Whatsapp
Select HTML elements with Selenium in Whatsapp

Time:10-26

Already sry for my English. Im writing script with selenium & python, what will choose all messages with photo in group. All messages boxes have this structure.

Repo: # there is bad code, check only if it interesting for you :)

https://github.com/AmirkulovOlzhas/whatsapp_group_photo_sort/tree/branch_test

div1
  span
     div2
  div3
     *** #info about message here(img=True)

#sry i cant put img here, need reputation 10. i can use only link

enter image description here

My code can check have div1 img or not. And also in theory it can select this cind of messages, code will select div1 after this if div3 img==True it have to select span/div2 and click it

#it was def with alot of if for sort divs, i cut it for clarity
messages = driver.find_element(
        By.XPATH, '//div[@]'.format("n5hs2j7m oq31bsqd lqec2n0o eu5j4lnj")). \
        find_elements(By.XPATH, '//div[@data-id]')
for div1 in messages:
   select_div3 = div1.find_element(By.XPATH, '//div[@data-testid="{}"]'.format('msg-container'))
   select_div2 = div1.find_element(By.XPATH, '//div[@]'.format('_3BK98'))
   if img_class in str(select_div3.get_attribute('class')):
       driver.execute_script("arguments[0].click();", select_div2)

its part of code have to check and select messages with img.

Problem: it can check div3(class=msg-container) from all div1, but it cant select span in all div1. Code will check div3 only on messages with photo, but can select any span, doesnt matter have it img or not.

enter image description here

message 2 have no photo, it was not processed with code, but it was sellected instead of message3 with photo message3 instead of mes4...

Can anyone tell me what am i did wrong?

What i can do to select span/div only in div1 only if div3 with img?

CodePudding user response:

i found answer. Problem was in

driver.execute_script("arguments[0].click();", select_div2)

its some how taked any span. so i tried write it more precisely

 driver.execute_script("arguments[0].click();", div1.find_element(By.CLASS_NAME, '_3BK98'))
  • Related