繁体   English   中英

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{"method":"xpath","selector":""}

[英]selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":""}

我正在制作一个机器人来登录不和谐并写一些东西,尽管当我登录时,有一个弹出消息我必须关闭(单击“X”按钮)才能转到我想要的频道。 但是通过我的代码,它并没有关闭弹出消息,它只是说

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="app-mount"]/div[5]/div[2]/div/div/div/div[1]/div[2]/button"}

这是我的代码:

from selenium import webdriver
from selenium.webdriver.common import by
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
import  time, os

class dankmemer_bot:
    def __init__(self):
        PATH = "C:\Program Files (x86)\chromedriver.exe"
        self.driver = webdriver.Chrome(PATH)
    
    def login(self):
        driver = self.driver
        driver.get('https://discord.com/channels/@me/12345678901234567')
        gmail = driver.find_element_by_name('email')
        gmail.send_keys('example@gmail.com')
        password = driver.find_element_by_name('password')
        password.send_keys(os.environ.get('password_example'))
        gmail.send_keys(Keys.RETURN)
    #The IMPORTANT part
    def close_tab(self):
        driver = self.driver
        button = driver.find_element_by_xpath('/html/body/div[1]/div[5]/div[2]/div/div/div/div[1]/div[2]/button')
        button.click()
        try:
            element = WebDriverWait(driver=10).until(
                EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div[5]/div[2]/div/div/div/div[1]/div[2]/button'))
            )
        except:
            driver.quit()

    def search_for_channel(self):
        driver = self.driver
        search_for_dankmemer = driver.find_element_by_class_name('searchBar-6Kv8R2')
        search_for_dankmemer.send_keys('dank-memer')

mybot = dankmemer_bot()    
mybot.login()
mybot.close_tab()
mybot.search_for_channel()

这是 html:

<button aria-label="Close" type="button" class="closeButton-ryIVwg close-hZ94c6 button-38aScr lookBlank-3eh9lL colorBrand-3pXr91 grow-q77ONN"><div class="contents-18-Yxp"><svg class="closeIcon-150W3V" aria-hidden="false" width="24" height="24" viewBox="0 0 24 24"><path fill="currentColor" d="M18.4 4L12 10.4L5.6 4L4 5.6L10.4 12L4 18.4L5.6 20L12 13.6L18.4 20L20 18.4L13.6 12L20 5.6L18.4 4Z"></path></svg></div></button>

要关闭弹出窗口或警报,我们可以使用此代码

driver.switch_to_alert()

然后编写代码关闭弹窗

例如:

driver.find_element_by_xpath('xpath')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM