简体   繁体   English

find_element() with Link_Text 无法使用 Selenium Python

[英]find_element() with Link_Text not working using Selenium Python

I am new to selenium whenever I try to find elements using id , name , xpath or anything it gives the same error每当我尝试使用idnamexpath或任何它给出相同错误的东西查找元素时,我都是 selenium 的新手

from selenium import webdriver
driver = webdriver.Safari()
driver.get('https://www.lambdatest.com/blog/selenium-safaridriver-macos/')
driver.find_element(By.Link_Text,'Webinar' )

It shows error by underlining By.它通过下划线显示错误。 I tried writing我试着写

driver.find_element_by_id('Webinar')

But it does not go through I tried different websites and with different elements like class, id but seems like there is a problem with finding elements by any method is there something that needs to installed to make it work?但它不是 go 通过我尝试了不同的网站和不同的元素,如 class,id 但似乎通过任何方法查找元素都有问题是否需要安装一些东西才能使其工作?

You have to take care of a couple of things here:你必须在这里处理几件事:

  • The supported locator strategy should be LINK_TEXT instead of Link_Text支持的定位器策略应该是LINK_TEXT而不是Link_Text

  • The link_text should be Webinars instead of Webinar . link_text应该是Webinars而不是Webinar

  • Effectively, your line of code should be:实际上,您的代码行应该是:

     driver.find_element(By.LINK_TEXT, 'Webinars' )

However, within the webpage there are multiple elements with innerText as Webinars .但是,在网页中有多个元素innerText as Webinars So you need to use a locator strategies which identifies the element uniquely within the DOM.因此,您需要使用定位器策略来唯一标识 DOM 中的元素。


Solution解决方案

Ideally, to locate the clickable element from the menu container with text as Webinars you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies :理想情况下,要从带有文本作为Webinars菜单容器中定位可点击元素,您需要为element_to_be_clickable()引入WebDriverWait并且您可以使用以下任一定位器策略

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.menu-menu-1-container a[href='https://www.lambdatest.com/webinar/']")))
  • Using XPATH :使用XPATH

     element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='menu-menu-1-container']//a[text()='Webinars']")))
  • 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

This这个

driver.find_element(By.Link_Text,'Webinar')

should be应该

driver.find_element(By.LINK_TEXT, 'Webinar')

Also, Import this from selenium.webdriver.common.by import By此外, from selenium.webdriver.common.by import By

If you are on Selenium4 then you should not really use:如果您使用的是 Selenium4,那么您不应该真正使用:

driver.find_element_by_id('Webinar')

use this instead:改用这个:

driver.find_element(By.ID, 'Webinar')

If you take a look at the source code, you'd see:如果你看一下源代码,你会看到:

class By(object):
    """
    Set of supported locator strategies.
    """

    ID = "id"
    XPATH = "xpath"
    LINK_TEXT = "link text"
    PARTIAL_LINK_TEXT = "partial link text"
    NAME = "name"
    TAG_NAME = "tag name"
    CLASS_NAME = "class name"
    CSS_SELECTOR = "css selector"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM