【问题标题】:unable to locate element in selenium in python [closed]无法在python中找到硒中的元素[关闭]
【发布时间】:2021-08-12 17:14:06
【问题描述】:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager


# Initiate the browser
browser  = webdriver.Chrome(ChromeDriverManager().install())

# Open the Website
browser.get("https://lpsc.codetantra.com/login.jsp")

codetantra_name="JE7-202@lpsc-ab.com"
codetantra_password="AB0707"

browser.find_element_by_name("loginId").send_keys(codetantra_name)

browser.find_element_by_name("password").send_keys(codetantra_password)

browser.find_element_by_class_name("pull-right").click()

browser.find_element_by_xpath("//*[@id='homeCenterDiv']/div/div[1]/div/div[2]/a").click()

【问题讨论】:

  • 您的回答没有以合适的格式显示代码,请修改它以便其他用户阅读。除了清楚之外,它还必须说明错误发生的位置、错误消息以及您之前尝试过的选项。在这种情况下,了解网页的 html 内容可能会有所帮助
  • 请帮助快速它显示没有找到这样的元素
  • 错误:消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//*[@id='homeCenterDiv']/div/div [1]/div/div[2]/a"}(会话信息:chrome=90.0.4430.212)

标签: python selenium google-chrome stack chromium


【解决方案1】:

您在定位元素之前缺少等待条件/延迟。
添加这个:

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

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "loginId"))).click()

之后就可以正常填写密码,点击登录按钮了

【讨论】:

  • 嘿,它不工作,我已经试过了!我说的是这个:browser.find_element_by_xpath("//*[@id='homeCenterDiv']/div/div[1]/div/div[2]/a").click()
  • 究竟是什么不起作用?
  • webdriver 延迟不起作用
  • 当某些东西不工作时,它通常会出现一些错误,而不是说“我不工作”
  • 消息:没有这样的元素:无法找到元素:{"method":"link text","selector":"lpsc.codetantra.com/secure/tla/m.jsp"}(会话信息:chrome=90.0.4430.212)它是给这个错误别生气我是新来的
【解决方案2】:

您需要引入 webdriverwait 才能完成这项工作。您要单击的元素是文本节点。虽然它不是普通的文本。 查看以下代码:

代码:

    driver = webdriver.Chrome("C:\\Users\\Inc\\Desktop\\Selenium+Python\\chromedriver.exe")
    driver.maximize_window()
    wait = WebDriverWait(driver, 30)
    driver.get('https://lpsc.codetantra.com/login.jsp')
    codetantra_name="JE7-202@lpsc-ab.com"
    codetantra_password="AB0707"
    
    driver.find_element_by_name("loginId").send_keys(codetantra_name)
    
    driver.find_element_by_name("password").send_keys(codetantra_password)
    
    driver.find_element_by_id("loginBtn").click()
    
    button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[role='button'][title='Click here to view Meetings']")))
    button.click()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    相关资源
    最近更新 更多