【问题标题】:selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:
【发布时间】:2020-07-03 14:41:24
【问题描述】:

我正在尝试编写一个使用多个帐户登录 Kahoot 的 python 脚本。我遇到了以下问题:我尝试使用 Selenium 找到“Enter”按钮并单击它。但我收到了这条消息:selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".enter-button__EnterButton-sc-1o9b9va-0 gSoXKU"} (Session info: chrome=83.0.4103.116) 我不知道为什么它不起作用。这是我的代码:

from selenium import webdriver

class kahoot_tab:
    def __init__(self):
        self.driver = webdriver.Chrome(r'C:\Users\...\chromedriver.exe')
        self.driver.get('https://kahoot.it/')

    def enter_pin(self, pin):
        self.driver.find_element_by_name('gameId').send_keys(pin)
        self.driver.find_element_by_class_name('enter-button__EnterButton-sc-1o9b9va-0 gSoXKU').click()

tab1 = kahoot_tab()
tab1.enter_pin('123456')

让我感到沮丧的是这个元素的存在,因为这是 HTML 代码:<button type="submit" value="Submit" class="enter-button__EnterButton-sc-1o9b9va-0 gSoXKU" data-functional-selector="join-game-pin"><span>Enter</span></button>

请帮帮我!

P.S.:在发送 pin 和单击按钮之间,我也尝试使用 time.sleep(100)

【问题讨论】:

    标签: python html python-3.x selenium


    【解决方案1】:

    您可以尝试使用输入按钮的完整 XPath,而不是使用其类名来选择它。话虽如此,您的代码应该是:

    from selenium import webdriver
    
    class kahoot_tab:
        def __init__(self):
            self.driver = webdriver.Chrome(r'C:\Users\...\chromedriver.exe')
            self.driver.get('https://kahoot.it/')
    
        def enter_pin(self, pin):
            self.driver.find_element_by_name('gameId').send_keys(pin)
            self.driver.find_element_by_xpath('/html/body/div/div/div/div/main/div/form/button').click()
    
    tab1 = kahoot_tab()
    tab1.enter_pin('123456')
    

    另外一件事是使用WebDriverWait。例如:

    enter_button = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "/html/body/div/div/div/div/main/div/form/button")))
    

    通过使用它,您在按下它之前等待元素完全加载。

    【讨论】:

    • 也感谢你,但由于我真的不知道 x 路径是如何工作的,所以我更喜欢 @FrianH 的方法。
    • 不客气!我更喜欢使用 XPath,因为元素的 id 或类在未来有更大的机会改变……嗯,至少在我过去测试过的一些网页中是这样。接受的答案仍然是一个很好的方法,我已经习惯了 XPath,这就是我一直在使用的,没有任何问题。
    【解决方案2】:

    你的目标元素有多个类名。

    enter-button__EnterButton-sc-1o9b9va-0gSoXKU

    使用.find_element_by_class_name是无法实现的,因为这个方法只针对单个类名。

    相反,对于多个类名,您可以使用 .find_element_by_css_selector 和此值 .enter-button__EnterButton-sc-1o9b9va-0.gSoXKU

    self.driver.find_element_by_css_selector('.enter-button__EnterButton-sc-1o9b9va-0.gSoXKU').click()
    

    CSS Selector Reference

    【讨论】:

    • @MichaelHofmann 欢迎,很高兴听到它有效。请参阅上面更新的答案以及更多解释,希望对您有所帮助。
    【解决方案3】:

    这是一个不依赖于元素的类或完整 XPATH 的替代方案

    from selenium.webdriver.common.by import By
    ...
    enter_button = driver.find_element(By.XPATH, "//button[contains(span, 'Enter')]")
    enter_button.click()
    

    您应该使用 Selenium 的 WebDriverWait 来确保 gameId 在尝试 send_keys() 之前可见。

    【讨论】:

    • 感谢您的回答,但我认为@frianH 的回答更简单。
    • 没关系,接受的答案是最能帮助你的:)。我提供了这个作为答案,因为它避免使用看起来像 CSS 模块并且将来可能会更改的类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2021-01-25
    • 2021-11-05
    • 1970-01-01
    相关资源
    最近更新 更多