【问题标题】:Send file via whatsapp from Python从 Python 通过 whatsapp 发送文件
【发布时间】:2019-06-30 05:07:29
【问题描述】:

我试图使用 Python Selenium Web 驱动程序发送一个文件。但我无法使用 python 找到发送按钮。我可以使用检查看到下面的 HTML:

在文件发送屏幕Whatsapp web screen shot 之后,我的 web whatsapp 屏幕也没有进展

我使用了以下代码:

fileToSend = file
#Get whatsapp contact
WebDriverWait(driver, wait).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.jN-F5')))
search = driver.find_element_by_css_selector('.jN-F5')
search.send_keys(user + Keys.ENTER)
# click to add
driver.find_element_by_css_selector('span[data-icon="clip"]').click()
# add file to send by file path
attach=driver.find_element_by_css_selector('input[type="file"]')
attach.send_keys(fileToSend) 
send=self.driver.find_element_by_class_name('yavlE')

以下是html源码:

 <div role="button" class="_3hV1n yavlE">
        <span data-icon="send-light" class="">
            <svg id="Layer_1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 28 24" width="28" height="24">
                <path fill="#FFF" d="M5.101 21.757L27.8 12.028 5.101 2.3l.011 7.912 13.623 1.816-13.623 1.817-.011 7.912z"></path>
            </svg>
        </span>
    </div>

请帮忙。

谢谢, 萨米尔

【问题讨论】:

  • 您遇到什么错误?可以分享一下错误日志吗?
  • 文件“C:\Users\ssingham\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py”,第 242 行,在 check_response 中引发异常类(消息,屏幕,堆栈跟踪)selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“类名”,“选择器”:“yavlE”}(会话信息:chrome = 72.0 .3626.81) (驱动信息: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.15063 x86_64)
  • 试试我的回答,让我知道状态,我已更改定位器

标签: python selenium whatsapp


【解决方案1】:

您在以下步骤中使用了错误的类名:

self.driver.find_element_by_class_name('yavlE')

用下面的定位器替换上面的行,然后重试:

self.driver.find_element_by_xpath("//div[contains(@class, 'yavlE')]");

你仍然得到 NoSuchElementException 然后尝试给一些延迟,如下所示:

from time import sleep
sleep(3)
self.driver.find_element_by_xpath("//div[contains(@class, 'yavlE')]");

我希望它有效...

【讨论】:

  • 嗨,阿里,感谢您的建议,但这并不能解决问题。我遇到了同样的错误。
  • 在这一步之前给一些延迟,比如sleep(3) - 导入它from time import sleep
  • 感谢 Ali,它现在可以工作了。 (只是为了我的理解)为什么简单的延迟会导致不必要的错误。
  • 欢迎...脚本试图在页面加载之前定位元素,因此您会收到 NoSuchElementException。如果可行,请接受我的回答并放弃投票。谢谢
  • 您的脚本是否也适用于self.driver.find_element_by_class_name('yavlE') 或仅适用于self.driver.find_element_by_xpath("//div[contains(@class, 'yavlE')]");
猜你喜欢
  • 2018-08-22
  • 1970-01-01
  • 2017-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多