gpt4 book ai didi

python - Python Selenium Web抓取返回找不到元素

转载 作者:行者123 更新时间:2023-12-03 16:49:41 24 4
gpt4 key购买 nike

我有以下网页,需要使用用户名和密码登录。

<div class="MuiFormControl-root MuiTextField-root MuiFormControl-marginNormal MuiFormControl-fullWidth"><label class="MuiFormLabel-root MuiInputLabel-root MuiInputLabel-formControl MuiInputLabel-animated MuiInputLabel-shrink MuiInputLabel-outlined Mui-required Mui-required" data-shrink="true">Username<span class="MuiFormLabel-asterisk MuiInputLabel-asterisk"> *</span></label><div class="MuiInputBase-root MuiOutlinedInput-root MuiInputBase-fullWidth MuiInputBase-formControl"><fieldset aria-hidden="true" class="jss298 MuiOutlinedInput-notchedOutline" style="padding-left: 8px;"><legend class="jss299" style="width: 70.25px;"><span>​</span></legend></fieldset><input aria-invalid="false" autocomplete="email username" class="MuiInputBase-input MuiOutlinedInput-input" required="" type="text" autocapitalize="none" value=""></div></div>"


我的代码如下

uname = driver.find_element_by_class_name('MuiFormControl-root MuiTextField-root MuiFormControl-marginNormal MuiFormControl-fullWidth')


但是,它返回以下错误:

NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".MuiFormControl-root MuiTextField-root MuiFormControl-marginNormal MuiFormControl-fullWidth"}
(Session info: chrome=75.0.3770.9


请你能帮我吗?

最佳答案

要将字符序列发送到所需字段,您必须为element_to_be_clickable()引入WebDriverWait,并且可以使用以下解决方案之一:


使用CSS_SELECTOR

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.MuiInputBase-root.MuiOutlinedInput-root.MuiInputBase-fullWidth.MuiInputBase-formControl input.MuiInputBase-input.MuiOutlinedInput-input[autocomplete='email username']"))).send_keys("bluemountains1979")

使用 XPATH

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='MuiInputBase-root MuiOutlinedInput-root MuiInputBase-fullWidth MuiInputBase-formControl']//input[@class='MuiInputBase-input MuiOutlinedInput-input' and @autocomplete='email username']"))).send_keys("bluemountains1979")

注意:您必须添加以下导入:

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

关于python - Python Selenium Web抓取返回找不到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56607823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com