📜  Python Selenium – 按文本查找元素

📅  最后修改于: 2022-05-13 01:55:08.058000             🧑  作者: Mango

Python Selenium – 按文本查找元素

验证用户给出的需求是否符合实际开发的软件产品的技术称为软件测试。此外,它还检查开发的最终软件产品是否没有错误。软件测试可以手动执行,也可以在软件测试工具的帮助下执行。通过内置工具自动完成的测试称为自动化测试。一个令人难以置信的Python库, Selenium可帮助您对软件进行自动化测试。在进行自动化测试时,您是否无法在所需网页中通过文本找到元素?那么,你就在一个合适的地方。在本文中,我们将讨论适当的步骤以及执行相同操作的示例。

示例:例如,考虑这个简单的页面源:

HTML

 
 
 
 


Python
from selenium import webdriver
from time import sleep


Python3
driver = webdriver.Chrome(
    executable_path="#path where you have installed webdriver")


Python3
driver.get("#Enter the website URL")


Python3
sleep(#duration)


Python3
driver.find_element_by_xpath(“//#tag name which contains word [contains( text(),
                             ‘#word to be found in a particular string’)]”)


Python
# Python Program to find element by text
  
#import webdriver
from selenium import webdriver
  
#import time
from time import sleep
  
# create webdriver object
driver = webdriver.Chrome(
    executable_path="C:\selenium\chromedriver_win32\chromedriver.exe")
  
# get the website
driver.get("http://bit.ly/vinayakgfg")
  
# sleep for some time
sleep(3)
  
# get element through text
driver.find_element_by_xpath("// a[contains(text(),\
'5 CHEAP HOLIDAY')]").click()
  
# sleep for some time
sleep(4)


创建驱动程序后,您可以使用以下方法掌握元素:

让我们通过实现来理解这一点:

第 1 步:首先,导入库、 selenium和 time。

Python

from selenium import webdriver
from time import sleep

第三步:接下来,通过可执行路径与web驱动建立连接。

蟒蛇3

driver = webdriver.Chrome(
    executable_path="#path where you have installed webdriver")

第 4 步:现在,获取要在其中查找元素的网站。

蟒蛇3



driver.get("#Enter the website URL")

第 5 步:然后,让Python休眠几秒钟,以便到那个时候,网页被加载。

蟒蛇3

sleep(#duration)

第 6 步:最后,通过给定网页上的文本找到所需的元素。

蟒蛇3

driver.find_element_by_xpath(“//#tag name which contains word [contains( text(),
                             ‘#word to be found in a particular string’)]”)

下面是完整的实现:

Python

# Python Program to find element by text
  
#import webdriver
from selenium import webdriver
  
#import time
from time import sleep
  
# create webdriver object
driver = webdriver.Chrome(
    executable_path="C:\selenium\chromedriver_win32\chromedriver.exe")
  
# get the website
driver.get("http://bit.ly/vinayakgfg")
  
# sleep for some time
sleep(3)
  
# get element through text
driver.find_element_by_xpath("// a[contains(text(),\
'5 CHEAP HOLIDAY')]").click()
  
# sleep for some time
sleep(4)

输出:

此外,搜索要查找的文本并显示如下输出。

更多用于定位单个元素的定位器:

Locators

Description

find_element_by_id The first element with the id attribute value matching the location will be returned.
find_element_by_name The first element with the name attribute value matching the location will be returned.
find_element_by_xpath The first element with the xpath syntax matching the location will be returned.
find_element_by_link_text The first element with the link text value matching the location will be returned.
find_element_by_partial_link_text The first element with the partial link text value matching the location will be returned.
find_element_by_tag_nameThe first element with the given tag name will be returned.
find_element_by_class_name The first element with the matching class attribute name will be returned.
find_element_by_css_selector The first element with the matching CSS selector will be returned.