I am trying to send messages automatically on LinkedIn. But I'm stuck in the phase of closing the chat window. How can I fix it? It's my first time using Selenium. Please help me.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://linkedin.com")
time.sleep(2)
username = driver.find_element(by=By.XPATH, value="//input[@name='session_key']")
password = driver.find_element(by=By.XPATH, value="//input[@name='session_password']")
username.send_keys("[email protected]")
password.send_keys("UMSUpos2022")
time.sleep(2)
submit = driver.find_element(by=By.XPATH, value="//button[@type='submit']").click()
time.sleep(2)
import random
n_pages = 3
for n in range(1, n_pages 1):
driver.get("https://www.linkedin.com/search/results/people/?network=["F"]&origin=FACETED_SEARCH&page=" str(n))
time.sleep(2)
all_buttons = driver.find_elements(By.TAG_NAME, 'button')
message_buttons = [btn for btn in all_buttons if btn.text == "Message"]
for i in range(2, 3):
#click on "Message" button
driver.execute_script("arguments[0].click();", message_buttons[i])
time.sleep(2)
#activate main div
main_div = driver.find_element(by=By.XPATH, value="//div[starts-with(@class, 'msg-form__msg-content-container')]")
driver.execute_script("arguments[0].click();", main_div)
#type message
paragraphs = driver.find_elements(By.TAG_NAME, 'p')
all_span = driver.find_elements(By.TAG_NAME, 'span')
all_span = [s for s in all_span if s.get_attribute("aria-hidden") == "true"]
idx = [*range(3,23,2)]
greetings = ["Hello", "Hi", "Hey"]
all_names = []
for j in idx:
name = all_span[j].text.split(" ")[0]
all_names.append(name)
greetings_idx = random.randint(0, len(greetings)-1)
message = greetings[greetings_idx] " " all_names[i] ", Sorry, I didn't mean to bother you, I'm just tesing a Linkedin capabilities... My apologies!:) This is not Bluu, this message is automated."
paragraphs[-5].send_keys(message)
time.sleep(2)
#send message
submit = driver.find_element(by=By.XPATH, value="//button[@type='submit']").click()
time.sleep(2)
#close div
close_button = driver.find_elements(By.CLASS_NAME, 'msg-overlay-bubble-header__control')
driver.execute_script("arguments[0].click();", close_button)
time.sleep(2)
<Here's my error message. This section below is my Error message come up.> There is a problem with closing the chat window. I even cannot find close trigger using finders in selenium. Please help me.
---------------------------------------------------------------------------
JavascriptException Traceback (most recent call last)
Input In [19], in <cell line: 5>()
46 #close div
47 close_button = driver.find_elements(By.CLASS_NAME, 'msg-overlay-bubble-header__control')
---> 48 driver.execute_script("arguments[0].click();", close_button)
49 time.sleep(2)
JavascriptException: Message: javascript error: arguments[0].click is not a function
(Session info: chrome=105.0.5195.128)
CodePudding user response:
You can use the selector [type="cancel-icon"].artdeco-button__icon
. You can either use
for button in driver.find_elements(By.CSS_SELECTOR, '[type="cancel-icon"].artdeco-button__icon'):
button.click()
# can run into errors if the button isn't visible or if another element is blocking it
or:
driver.execute_script("document.querySelectorAll('[type=\"cancel-icon\"].artdeco-button__icon').forEach(e => e.click())")
Which will close all open menus/chat boxes.
CodePudding user response:
I tried XPath using id and it worked.
driver.find_element(By.XPATH, '//button[@id="ember304"]').click()