【问题标题】:NoSuchElementException while joining whatsapp group using selenium and python3.8使用 selenium 和 python3.8 加入 whatsapp 组时出现 NoSuchElementException
【发布时间】:2021-08-16 12:15:51
【问题描述】:

我正在尝试使用 selenium 和 python 自动加入 WhatsApp 组。我几乎可以到达最后一页,但找不到加入群组按钮。这是我的python代码。请注意,首先我使用 5 秒的等待时间登录 WhatsApp。

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
  
# create webdriver object
driver = webdriver.safari.webdriver.WebDriver(quiet=False)

driver.get("https://web.whatsapp.com/")
import time 
time.sleep(5)


driver.get("https://chat.whatsapp.com/invite/ANY_WA_GROUP_LINK")

# create action chain object
action = ActionChains(driver)

element = driver.find_element_by_link_text("Join Chat")
action.click(on_element = element)
action.perform()

element = driver.find_element_by_link_text("use WhatsApp Web")
action.click(on_element = element)
action.perform()

element = driver.find_element_by_link_text("Join group")
action.click(on_element = element)
action.perform()

以下是我得到的错误。

Traceback (most recent call last):
  File "basic.py", line 25, in <module>
    element = driver.find_element_by_link_text("Join group")
  File "/Users/Home/WorkSpace/anaconda3/envs/env_auto/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 428, in find_element_by_link_text
    return self.find_element(by=By.LINK_TEXT, value=link_text)
  File "/Users/Home/WorkSpace/anaconda3/envs/env_auto/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/Users/Home/WorkSpace/anaconda3/envs/env_auto/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Users/Home/WorkSpace/anaconda3/envs/env_auto/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message:

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver automation


    【解决方案1】:

    如果您的定位器是正确的,您将错过element = driver.find_element_by_link_text("Join Chat") 之前的等待/延迟。
    所以,只需尝试添加

    time.sleep(5)
    

    之前

    element = driver.find_element_by_link_text("Join Chat")
    

    更新: 试试这个:

    element = driver.find_element_by_xpath('//div[contains(text(),'Join group')]')
    

    【讨论】:

    • 不。没用。有一些网页问题。页面发生了变化,而源仍然是旧的或类似的。
    • 好的,在页面更改之后,一旦它最终稳定下来,那里有link_text("Join Chat")匹配的元素吗?也许您只是使用了错误的定位器
    • 这是定位器的问题。但无法导航并点击按钮
    • &lt;div role="button" tabindex="0" class="_1dwBj _3xWLK"&gt;&lt;div class="_3NCXc _1Yw2u _FUG3 _2HGw4"&gt;&lt;div class="VtaVl -TvKO"&gt;Join group&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
    • 如何点击带有角色按钮的 div
    猜你喜欢
    • 2022-01-17
    • 2020-11-04
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多