无法找到元素(硒)

时间:2018-09-22 09:50:16

标签: selenium web-scraping whatsapp

我正试图通过在whatsapp上通过硒向哥哥发送重复短信来惹恼我。问题是,即使我告诉它,它也没有明确等待2秒:button=WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CLASS_NAME, '_35EW6'))) 它向他发送消息一次,两次或三次,然后停止。

代码如下:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver=webdriver.Chrome("C:\setups\chromedriver\chromedriver.exe")

driver.get("https://web.whatsapp.com/")

name=input("Enter the name of person or group: ")
msg=input("Enter a message: ")
count=eval(input("How many times: "))

input("Wait for ENTER")

user=driver.find_element_by_xpath('//span[@title="{}"]'.format(name))
user.click()
msg_box=driver.find_element_by_class_name("_1Plpp")
for i in range(count):
    msg_box.send_keys(msg)
    msg_box.send_keys(Keys.RETURN)
    button=driver.find_element_by_class_name('_35EW6')
    # button.click()
    button=WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CLASS_NAME, '_35EW6')))
    button.click()

结果如下:

DevTools listening on ws://127.0.0.1:52056/devtools/browser/23246055-9f5d-49f7-9677-01b3ab2e8905
Enter the name of person or group: Muhammad Faizan
Enter a message: Hacked
How many times: 15
Wait for ENTER
Traceback (most recent call last):
  File "c:/Users/mnauf/Desktop/AI/practice/ksew.py", line 22, in <module>
    button=driver.find_element_by_class_name('_35EW6')
  File "C:\Users\mnauf\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "C:\Users\mnauf\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\mnauf\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\mnauf\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"_35EW6"}
  (Session info: chrome=69.0.3497.100)
  (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.17134 x86_64)

1 个答案:

答案 0 :(得分:0)

我只是删除了 1. from selenium.webdriver.common.keys import Keys 2. msg_box.send_keys(Keys.RETURN) 3. button=driver.find_element_by_class_name('_35EW6') 而且有效。