[英]How do I select the an element without an ID/text_link/name in selenium?
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
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.