Whatsapp Web Selenium Headless Chrome 不工作
我有一个在登录 WhatsApp Web 的 PythonAnywhere 上运行的脚本中使用的函数:
def send_message_whatsapp(contact,message):
# Send to whatsapp
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36'
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument(f'user-agent={user_agent}')
options.add_argument('--window-size=1920,1080')
options.add_argument("--lang=en")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
#options.add_argument("--proxy-server='direct://'")
#options.add_argument("--proxy-bypass-list=*")
#options.add_experimental_option('useAutomationExtension', False)
#options.add_argument("--disable-gpu")
#options.add_argument('--disable-infobars')
options.add_argument("--user-data-dir=whatsapp_profile")
#options.add_argument("disable-extensions")
driver = webdriver.Chrome(executable_path='chromedriver', options=options)
driver.get("https://web.whatsapp.com/")
time.sleep(20)
driver.get_screenshot_as_file("whatsappconfirmlogin.png")
time.sleep(20)
wait = WebDriverWait(driver, 100)
driver.maximize_window()
# Find the group name or whatsapp contact
x_arg = f'//span[contains(@title,"{contact}")]'
try:
wait.until(EC.element_to_be_clickable((By.XPATH,x_arg)))
driver.find_element_by_xpath(f'//span[@title="{contact}"]').click()
driver.get_screenshot_as_file("whatsappconfirmgroupclick.png")
inp_xpath = '//div[@class="_13NKt copyable-text selectable-text"][@data-tab="10"]'
input_box = wait.until(EC.presence_of_element_located((
By.XPATH, inp_xpath)))
for item in message:
input_box.send_keys(item + Keys.SHIFT + Keys.ENTER)
time.sleep(2)
button = driver.find_element_by_class_name('_4sWnG')
button.send_keys(Keys.ENTER)
# Close the application
driver.quit()
print("Successfully sent whatsapp message")
except:
print("Unable to find Whatsapp Group. Logging out...")
driver.get_screenshot_as_file("whatsapptesterror.png")
driver.quit()
使用的脚本在最近的 Whatsapp Web 更新之前工作。现在,只是无法登录。查看保存的屏幕截图,我可以看到它尝试连接,但随后显示消息“确保您的计算机具有活动的互联网连接”。
当我尝试在本地桌面上运行类似的代码(该代码不在无头 Chrome 中运行)时,它似乎工作得很好。
def send_message_whatsapp(contact, message):
options = webdriver.ChromeOptions();
options.add_argument(r'--user-data-dir=path')
#driver = webdriver.Chrome(chrome_options=options)
driver = webdriver.Chrome(r'chromedriver.exe', options=options)
driver.get("https://web.whatsapp.com/")
time.sleep(10)
wait = WebDriverWait(driver, 30)
# Find the group name or whatsapp contact
x_arg = f'//span[contains(@title,"{contact}")]'
group_title = wait.until(EC.presence_of_element_located((By.XPATH, x_arg)))
group_title.click()
inp_xpath = '//div[@class="_13NKt copyable-text selectable-text"][@data-tab="10"]'
input_box = wait.until(EC.presence_of_element_located((
By.XPATH, inp_xpath)))
for item in message:
input_box.send_keys(item + Keys.SHIFT + Keys.ENTER)
time.sleep(2)
button = driver.find_element_by_class_name('_4sWnG')
time.sleep(2)
button.click()
# Close the application
driver.close()
I have a function that I use within a script that runs on PythonAnywhere which logs into WhatsApp Web:
def send_message_whatsapp(contact,message):
# Send to whatsapp
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36'
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument(f'user-agent={user_agent}')
options.add_argument('--window-size=1920,1080')
options.add_argument("--lang=en")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
#options.add_argument("--proxy-server='direct://'")
#options.add_argument("--proxy-bypass-list=*")
#options.add_experimental_option('useAutomationExtension', False)
#options.add_argument("--disable-gpu")
#options.add_argument('--disable-infobars')
options.add_argument("--user-data-dir=whatsapp_profile")
#options.add_argument("disable-extensions")
driver = webdriver.Chrome(executable_path='chromedriver', options=options)
driver.get("https://web.whatsapp.com/")
time.sleep(20)
driver.get_screenshot_as_file("whatsappconfirmlogin.png")
time.sleep(20)
wait = WebDriverWait(driver, 100)
driver.maximize_window()
# Find the group name or whatsapp contact
x_arg = f'//span[contains(@title,"{contact}")]'
try:
wait.until(EC.element_to_be_clickable((By.XPATH,x_arg)))
driver.find_element_by_xpath(f'//span[@title="{contact}"]').click()
driver.get_screenshot_as_file("whatsappconfirmgroupclick.png")
inp_xpath = '//div[@class="_13NKt copyable-text selectable-text"][@data-tab="10"]'
input_box = wait.until(EC.presence_of_element_located((
By.XPATH, inp_xpath)))
for item in message:
input_box.send_keys(item + Keys.SHIFT + Keys.ENTER)
time.sleep(2)
button = driver.find_element_by_class_name('_4sWnG')
button.send_keys(Keys.ENTER)
# Close the application
driver.quit()
print("Successfully sent whatsapp message")
except:
print("Unable to find Whatsapp Group. Logging out...")
driver.get_screenshot_as_file("whatsapptesterror.png")
driver.quit()
The script used to work before the recent Whatsapp Web update. Now, it just fails to login. Looking at the saved screenshots, I can see that it tries to connect, but then it shows the message "Make sure your computer has an active internet connection".
When I try to run a similar code on my local desktop where it does not run in headless chrome, it seems to work just fine.
def send_message_whatsapp(contact, message):
options = webdriver.ChromeOptions();
options.add_argument(r'--user-data-dir=path')
#driver = webdriver.Chrome(chrome_options=options)
driver = webdriver.Chrome(r'chromedriver.exe', options=options)
driver.get("https://web.whatsapp.com/")
time.sleep(10)
wait = WebDriverWait(driver, 30)
# Find the group name or whatsapp contact
x_arg = f'//span[contains(@title,"{contact}")]'
group_title = wait.until(EC.presence_of_element_located((By.XPATH, x_arg)))
group_title.click()
inp_xpath = '//div[@class="_13NKt copyable-text selectable-text"][@data-tab="10"]'
input_box = wait.until(EC.presence_of_element_located((
By.XPATH, inp_xpath)))
for item in message:
input_box.send_keys(item + Keys.SHIFT + Keys.ENTER)
time.sleep(2)
button = driver.find_element_by_class_name('_4sWnG')
time.sleep(2)
button.click()
# Close the application
driver.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论