Selenium-如何填充值并单击按钮



我今天从硒开始,在查找这些元素、在第一个元素中填充值并单击第二个元素时遇到了问题。

<input class="inputDefault-_djjkz input-cIJ7To" name="username" type="text" placeholder="Che nome vuoi usare?" maxlength="999" value="">
<input class="inputDefault-3JxKJ2 input-3ITkQf" type="checkbox" style="width: 24px; height: 24px;">

这是我的代码

from selenium import webdriver
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

# Content to fill - <input class="inputDefault-_djjkz input-cIJ7To" name="username" type="text" placeholder="Che nome vuoi usare?" maxlength="999" value="">
# {"method":"css selector","selector":"[name="Che nome vuoi usare?"]"}
# Checkbox - <input class="inputDefault-3JxKJ2 input-3ITkQf" type="checkbox" style="width: 24px; height: 24px;">
driver = webdriver.Chrome()
driver.get("https://discord.gg/NGvXRfrmUE")
name = driver.find_element_by_class_name("inputDefault-_djjkz input-cIJ7To").send_keys("Test")
button = driver.find_element_by_class_name("inputDefault-3JxKJ2 input-3ITkQf").click

我得到了第一个的例外

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".inputDefault-_djjkz input-cIJ7To"}

这是第二个

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".inputDefault-3JxKJ2 input-3ITkQf"}

由于name可用,因此应该使用name attribute

driver.find_element_by_name('username').send_keys("Test")

我可以在我的末尾看到这个HTML:

<input class="inputDefault-_djjkz input-cIJ7To inputField-4g7rSQ" name="email" type="text" placeholder="" aria-label="Email or Phone Number" autocomplete="off" maxlength="999" spellcheck="false" value="">

所以这里的name就是email,所以应该试试:

driver.find_element_by_name('email').send_keys("Test")

更新1:

driver.get("https://discord.com/invite/NGvXRfrmUE")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.NAME, "username"))).send_keys('Test')

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

相关内容

  • 没有找到相关文章

最新更新