问题
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.
回答1:
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)
回答2:
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)
来源:https://stackoverflow.com/questions/60614714/python-selenium-whatsapp-search-button