无法通过id,名称或链接文本或WebDriverWait()在selenium中找到元素。每次都没有抛出这样的错误异常 [英] Can't find element in selenium by id, name or link text or WebDriverWait(). No such error exception thrown every time

查看:303
本文介绍了无法通过id,名称或链接文本或WebDriverWait()在selenium中找到元素。每次都没有抛出这样的错误异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<a name="DERIVED_SSS_SCL_SSS_ENRL_CART$276$" id="DERIVED_SSS_SCL_SSS_ENRL_CART$276$" ptlinktgt="pt_peoplecode" tabindex="126" href="javascript:submitAction_win0(document.win0,'DERIVED_SSS_SCL_SSS_ENRL_CART$276$');" class="SSSHYPERLINKBOLDSMALL">Enrollment Shopping Cart</a>

我想要找到的元素。

背景:我正在尝试自动化课程注册。有问题的页面必须通过一些安全措施访问,绝对不是典型的网页。

Background: I'm trying to automate class registration. The page in question must be accessed by some security measures and is definitely not a typical web page.

首先,我试过这个:

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

driver = webdriver.Chrome()
username = input("Username: ")
password = input("Password: ")

username_box = driver.find_element_by_name("j_username")
username_box.send_keys(username)

password_box = driver.find_element_by_name("j_password")
password_box.send_keys(password)
password_box.send_keys(Keys.RETURN) # Now, we are at the page in question after signing on.

我试过这个:

to_shopping_cart = driver.find_element_by_link_text("Enrollment Shopping Cart")

with find_element_by_link_text ,find_element_by_partial_link_text,find_element_by_id,find_element_by_name以及此处文档中列出的一些其他选项( https://selenium-python.readthedocs.org/api.html#selenium.webdriver.remote.webdriver.WebDriver.find_element_by_link_text )。所有这些都抛出没有这样的元素。

with find_element_by_link_text, find_element_by_partial_link_text, find_element_by_id, find_element_by_name and a few other options listed in the docs here (https://selenium-python.readthedocs.org/api.html#selenium.webdriver.remote.webdriver.WebDriver.find_element_by_link_text). ALL of these threw "no such element".

接下来,我尝试使用WebDriverWait。这继续抛出TimeOut例外。

Next, I tried using WebDriverWait. This kept throwing "TimeOut exception".

当前理论:

1它与受保护的标志有关这是一个非常奇怪的网站,完全不直观,彼此之间有大量的元素。

1 It has to do with the protected sign in. It's a really strangely built site, not intuitive at all, tons and tons of elements within one another.

2它与周围的其他元素有关。我肯定会上传那些,如果我需要,它只是一个巨大的html块,所以我想暂时让问题尽可能简洁。

2 It has to do with the other elements around it. I'll definitely upload those if I need to, it's just a huge block of html so I wanted to keep the question as concise as possible for the time being.

3我考虑过使用带有href操作的driver.get(),但只是重新加载了网页。所以这也不起作用。

3 I thought about using driver.get() with the href action but that just reloads the web page. So that doesn't work either.

切换到正确的iFrame:

By switching to the proper iFrame with:

proper_frame_element = driver.find_element_by_name('TargetContent')
driver.switch_to_frame(proper_frame_element)


推荐答案

根据评论中给出的信息,所需元素位于iframe中。这意味着您需要在进行搜索之前切换到iframe

According the information given in comments, the desired element is inside an iframe. This means that you need to switch to the iframe before making a search:

driver.switch_to.frame("frame name or id")
to_shopping_cart = driver.find_element_by_link_text("Enrollment Shopping Cart")

然后,如果您需要切换到默认内容,请使用:

Then, if you need to switch to a default content, use:

driver.switch_to.default_content()

这篇关于无法通过id,名称或链接文本或WebDriverWait()在selenium中找到元素。每次都没有抛出这样的错误异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆