如何使用 Selenium 和 Python 识别元素
How to identify the element using Selenium and Python
我在一个项目上工作了一天,一直在修补 Selenium (Python)。我在尝试点击网页的 link 时卡住了,因为我无法通过迄今为止发现的任何方式检索该元素。
link是这样的:
<p>
<a href="0_fr.asp?contenido=0actunor.asp" target="mainFrame"><font size="3" face="Arial" color="#CC9900">
<b>Base de datos de Disposiciones Vigentes</b></font></a>
</font>
</p>
现在,到目前为止我已经尝试了这些东西,并且都给我 NoSuchElementException:
- driver.find_element_by_css_selector("a[href=0_fr.asp?contenido=0actunor.asp]")
- driver.find_element_by_css_selector('div:contains("Base de datos de Disposiciones Vigentes")')
- 与 1) 相同,但将 CSS 选择器单独复制并粘贴为字符串
- by link 文本和部分 link 文本使用 和
之间的内容
- 通过使用 id 以防万一它做了什么。
在其他 Whosebug 问题和在线指南中发现了更多关于构建我自己的 css 选择器等的问题,但我不知道任何 HTML 或 CSS 所以它一直跟随他们有点困难。
如果需要,我可以提供更多信息,但我无法 link 网页,因为您必须登录,这是工作的事情。
非常欢迎任何帮助或搜索更多信息的地方!
已解决:问题是 link 在一个框架内,而这个框架在另一个框架内。我只需要使用 driver.switch_to.frame("frameName") 两次,然后正常使用 css_selector 并正常工作。
尝试以下命令。
driver.find_element_by_link_text("Base de datos de Disposiciones Vigentes")
该元素是 <a>
元素,您将通过所有可能的方式进行交互,即单击它。理想情况下使用 you have to induce for the element_to_be_clickable()
and you can use either of the following :
在任何元素上调用 click()
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='contenido'][href*='actunor'][target='mainFrame'] b"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, 'contenido') and contains(@href, 'actunor')][@target='mainFrame']//b[text()='Base de datos de Disposiciones Vigentes']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
更新
当您观察到 TimeoutException 导致 WebDriverWait 您需要解决 first following the discussion in
我在一个项目上工作了一天,一直在修补 Selenium (Python)。我在尝试点击网页的 link 时卡住了,因为我无法通过迄今为止发现的任何方式检索该元素。
link是这样的:
<p>
<a href="0_fr.asp?contenido=0actunor.asp" target="mainFrame"><font size="3" face="Arial" color="#CC9900">
<b>Base de datos de Disposiciones Vigentes</b></font></a>
</font>
</p>
现在,到目前为止我已经尝试了这些东西,并且都给我 NoSuchElementException:
- driver.find_element_by_css_selector("a[href=0_fr.asp?contenido=0actunor.asp]")
- driver.find_element_by_css_selector('div:contains("Base de datos de Disposiciones Vigentes")')
- 与 1) 相同,但将 CSS 选择器单独复制并粘贴为字符串
- by link 文本和部分 link 文本使用 和 之间的内容
- 通过使用 id 以防万一它做了什么。
在其他 Whosebug 问题和在线指南中发现了更多关于构建我自己的 css 选择器等的问题,但我不知道任何 HTML 或 CSS 所以它一直跟随他们有点困难。 如果需要,我可以提供更多信息,但我无法 link 网页,因为您必须登录,这是工作的事情。
非常欢迎任何帮助或搜索更多信息的地方!
已解决:问题是 link 在一个框架内,而这个框架在另一个框架内。我只需要使用 driver.switch_to.frame("frameName") 两次,然后正常使用 css_selector 并正常工作。
尝试以下命令。
driver.find_element_by_link_text("Base de datos de Disposiciones Vigentes")
该元素是 <a>
元素,您将通过所有可能的方式进行交互,即单击它。理想情况下使用 element_to_be_clickable()
and you can use either of the following
click()
使用
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='contenido'][href*='actunor'][target='mainFrame'] b"))).click()
使用
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, 'contenido') and contains(@href, 'actunor')][@target='mainFrame']//b[text()='Base de datos de Disposiciones Vigentes']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
更新
当您观察到 TimeoutException 导致 WebDriverWait 您需要解决