简体   繁体   English

我如何 select selenium 中没有 ID/text_link/name 的元素?

[英]How do I select the an element without an ID/text_link/name in selenium?

HTML of the website网站的 HTML

The HTML code of the element I want to select:我想要的元素的 HTML 代码 select:

   <a href="../s-reminderNotice.asp?fname=b%2D3c%2DpLessonBooking%2Easp%3Flimit%3Dpl" class="sidelinkbold" 
   target="mainFrame" 
   onmouseover=" window.status='Practical Training Booking'; 
   return true" onmouseout="window.status=' ';
   return true">Booking without Fixed Instructor</a>

I would like to select the element of this "Booking without Fixed Instructor";我想 select 这个“没有固定教练的预订”的元素; however there isn't an ID/link_text/name for this element.但是这个元素没有 ID/link_text/name。 How can I direct the site to the HREF.如何将站点定向到 HREF。 Is there a way to use xpath to locate it, the element is in the column on the left of the website so there are many other elements of the same class.有没有办法使用 xpath 来定位它,该元素位于网站左侧的列中,因此还有许多其他相同的 class 元素。 (see pic.) (见图)

I tried this and it returned:我试过了,它返回了:

AttributeError: 'WebDriver' object has no attribute 'findElement'

Code:代码:

driver.findElement(By.xpath("//a[@href='THE LINK']")).click();

SOLVED;解决了; ELEMENT WAS IN AN IFRAME;元素在 IFRAME 中; HAD TO SWITCH FRAME TO ACCESS ELEMENT必须切换框架以访问元素

AttributeError: 'WebDriver' object has no attribute 'findElement'

you are using python so driver dowsn't have findElemnt method, it is find_Element您正在使用 python 所以驱动程序没有 findElemnt 方法,它是 find_Element

https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html?highlight=webdriver%20class#selenium.webdriver.remote.webdriver.WebDriver.find_element_by_class_name https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html?highlight=webdriver%20class#selenium.webdriver.remote.webdriver.WebDriver.find_element_by_class_name

use:

    driver.find_element(By.XPATH("//a[contains(string(),'Booking without Fixed Instructor')]")

To click on the element with text as Booking without Fixed Instructor you can use either of the following Locator Strategies :要单击文本为Booking without Fixed Instructor的元素,您可以使用以下任一Locator Strategies

  • Using link_text :使用link_text

     driver.find_element(By.LINK_TEXT, "Booking without Fixed Instructor")
  • Using partial_link_text :使用partial_link_text

     driver.find_element(By.PARTIAL_LINK_TEXT, "Booking without Fixed Instructor").click()
  • Using css_selector :使用css_selector

     driver.find_element(By.CSS_SELECTOR, "a.sidelinkbold[href*='s-reminderNotice']").click()
  • Using xpath :使用xpath

     driver.find_element(By.XPATH, "//a[@class='sidelinkbold' and contains(@href, 's-reminderNotice')][contains(., 'Booking without Fixed Instructor')]").click()

The desired element is a dynamic element, so ideally to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :所需元素是动态元素,因此理想情况下,单击需要为element_to_be_clickable()诱导WebDriverWait的元素,您可以使用以下任一Locator Strategies

  • Using LINK_TEXT :使用LINK_TEXT

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Booking without Fixed Instructor"))).click()
  • Using PARTIAL_LINK_TEXT :使用PARTIAL_LINK_TEXT

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Booking without Fixed Instructor"))).click()
  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.sidelinkbold[href*='s-reminderNotice']"))).click()
  • Using XPATH :使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='sidelinkbold' and contains(@href, 's-reminderNotice')][contains(., 'Booking without Fixed Instructor')]"))).click()
  • Note : You have to add the following imports:注意:您必须添加以下导入:

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

References参考

You can find a couple of relevant discussions on NoSuchElementException in:您可以在以下位置找到一些关于NoSuchElementException的相关讨论:

Try this xpath:试试这个 xpath:

//a[text()[contains(.,'Booking without Fixed Instructor')]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过Selenium / Python选择没有ID的Web元素 - How do I select a web element with no ID through Selenium/Python 如何选择具有该名称的Microsoft CRM表单上的Last Name元素 <input id = “lastname_i”…> 使用Python selenium驱动程序? - How do I select the Last Name element on Microsoft CRM form that has the name <input id = “lastname_i”…> using Python selenium driver? 无法通过ID,名称或链接文本或WebDriverWait()找到硒中的元素。 每次都不会抛出这样的错误异常 - Can't find element in selenium by id, name or link text or WebDriverWait(). No such error exception thrown every time Selenium Web 驱动程序:如何单击没有 ID、NAME 的图像按钮 - Selenium Web driver: how do I click on image button without ID, NAME how do I select xpath image without a class name using selenium in python? - how do I select xpath image without a class name using selenium in python? 如何单击硒中元素隐藏的链接? - How do I click link hidden by element in selenium? 如何在 Python Selenium 的弹出窗口中选择一个元素 - How do I select an element on a popup in Python Selenium 如何使用 Selenium 命名 CSS 元素 select? - How do I select a named CSS element with Selenium? 如何在 selenium 中通过 class 名称查找元素? - How do I find element by class name in selenium? 如何在 Selenium 中通过 id 查找元素? - How do I find an element by its id in Selenium?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM