【发布时间】:2021-09-25 10:39:58
【问题描述】:
我今天开始使用 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"}
【问题讨论】:
-
你试过这个:打开开发工具并选择元素,然后右键单击并复制xpath。然后 driver.find_element_by_xpath('path')
标签: python css selenium selenium-webdriver selenium-chromedriver