0

I am searching contacts through phone number and sending message in whatsapp using python selenium. But i am not able to find the name, when I input in the search using send keys, it is getting deleted, when it goes to next element. I want to know how to use alternate driver.executescript or where the attribute searched stays, and I can find the name of that .

 v_elem=driver.find_element_by_class_name('_3u328')
 v_elem.send_keys(phone))
 driver.find_element_by_css_selector('span[class="_19RFN _1ovWX _F7Vk"]').click()

When the script send keys, it deletes before finding the next css .. as that is where the name of the number exist. How to find this.

supputuri
  • 13,644
  • 2
  • 21
  • 39
vik
  • 1
  • 2
  • Please post HTML section of that button – Wonka Mar 10 '20 at 10:03
  • Find the associated event to the field and trigger the same using js. Refer to [this](https://stackoverflow.com/questions/55977388/textbox-events/55978587#55978587) post to know how to find the associated event. – supputuri Mar 10 '20 at 14:10
  • [code]
    [/code] this is html section i am trying to key in value for phone number.
    – vik Mar 10 '20 at 15:35
  • i tried the following, instead of send key, but execute script is not setting the value for this element ,i want to search number&select name javaScript = driver.find_element_by_xpath("//div[@class='_3u328 copyable-text selectable-text']") #driver.execute_script("arguments[0].value ='+phone+'",javaScript) – vik Mar 10 '20 at 15:38
  • Hi supputuri, in that case, how to stop events.. i saw that 5 events associated to this particular one..Can you guide me, how to do. i saw d post, i could not entirely understand. – vik Mar 10 '20 at 16:15

2 Answers2

0

Here is the sample script to trigger onclick event.

ele = driver.find_element_by_xpath("//div[@class='_3u328 copyable-text selectable-text']") driver.execute_script("arguments[0].value =123",ele)
# now fire the even associated with the element here
driver.execute_script("arguments[0].dispatchEvent(new Event('click', {'bubbles': true,'cancelable': true}));",ele)
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • dirver.execute_script is not putting value in DOM, eventhough no error is thrown.i am using firefox as browser. Hence the value is not entered in the searchbox at all. – vik Mar 11 '20 at 08:28
  • if i do driver.execute_script("arguments[0].setAttribute.value('value','123');",ele)
    – vik Mar 11 '20 at 08:32
  • but the value is not updated , when i try driver.execute_script , above is the html code pasted.. i tried yours and also checked whether attribute changing. but attribute changes, not the value updates .. i am not knowing y? – vik Mar 11 '20 at 08:34
0

I had the same problem, and I had to solve it using an inelegant procedure.

I used a mouse-bot to click on the search button, then I used a keyboard-bot to type the name I want to search for. You will have to adapt "x" and "y" for your screen.

This specific segment of the code are shown below.

import time
import win32api, win32con
import keyboard

x=179
y=287
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

time.sleep(1)
keyboard.write('Contact_name', delay=0.1)