无法使用Selenium单击网站

2024-09-20 22:52:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我以为我明白这个概念,但我想我不明白

我想从HTML页面的选项卡列表中单击一个按钮

<div id="subTabs2">
<div id="subTabs">
            
<ul>

<li>
<a class="currentTab" id="sub_tab_timeclock_today" href="?p=timeclock:today">Today</a>
</li>

<li>
<a id="sub_tab_timeclock_my_timesheet" href="?p=timeclock:my_timesheet">My timesheet</a>
</li>

#list continues ....

我想点击标签“sub_tab_timeclock_my_timesheet”,所以在我的代码中我点击了

# some code

driver = webdriver.Chrome(driverPath)

driver.get(url)


username = driver.find_element_by_id("user_handle")
password = driver.find_element_by_id("user_password")

username.send_keys("myUser")
password.send_keys("myPass")

driver.find_element_by_class_name("button1").click()

driver.find_element_by_id("sub_tab_timeclock_my_timesheet").click()

我确实通过了登录页面,但我不明白为什么我的代码不能用于单击ID为“sub_tab\u timeclock\u my_timesheet”的选项卡“my timesheet”

错误消息是:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="sub_tab_timeclock_my_timesheet"]"}
  (Session info: chrome=85.0.4183.83)

我做错了什么

谢谢


Tags: dividbymydriverpassword页面li
1条回答
网友
1楼 · 发布于 2024-09-20 22:52:34

看起来你可能遇到了时间问题。尝试在driver.find_element_by_class_name("button1").click()driver.find_element_by_id("sub_tab_timeclock_my_timesheet").click()之间实现等待

为此,您需要执行以下操作:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "sub_tab_timeclock_my_timesheet"))
    )
finally:
    driver.quit()

由于我是.NET开发人员,因此对Python代码不太确定,但请查看本文中的语法细节: https://selenium-python.readthedocs.io/waits.html

相关问题 更多 >

    热门问题