在python中通过链接文本(忽略字符串中的符号)查找元素
我正在尝试将web上的字符串与我传递的字符串匹配。 web上的字符串包含以下符号-,:;我传递的是没有符号的字符串在python中通过链接文本(忽略字符串中的符号)查找元素,python,selenium,xpath,Python,Selenium,Xpath,我正在尝试将web上的字符串与我传递的字符串匹配。 web上的字符串包含以下符号-,:;我传递的是没有符号的字符串 import time from selenium import webdriver from selenium.webdriver.common.keys import Keys name="Ant Man and the Wasp" driver=webdriver.Firefox() driver.get("https://www.movieshunters.com") se
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
name="Ant Man and the Wasp"
driver=webdriver.Firefox()
driver.get("https://www.movieshunters.com")
search=driver.find_element_by_id('s')
search.clear()
search.send_keys(name)
driver.find_element_by_class_name('search-button').submit()
driver.find_element_by_link_text(name.title()).click()
<a href="https://www.movieshunters.com/movies/ant-man-and-the-wasp/">Ant-Man and the Wasp</a>
导入时间
从selenium导入webdriver
从selenium.webdriver.common.keys导入密钥
name=“蚂蚁和黄蜂”
driver=webdriver.Firefox()
驱动程序。获取(“https://www.movieshunters.com")
搜索=驱动程序。按\u id('s')查找\u元素\u
search.clear()
搜索。发送密钥(名称)
驱动程序。通过类名称(“搜索按钮”)查找元素。提交()
驱动程序。通过链接文本(name.title())查找元素。单击()
您可以使用xpath实现这一点。翻译是你必须使用的方法
driver.find_element_by_xpath("//a[translate(.,'-',' ') = 'Ant Man and the Wasp']").get_attribute('href')
在代码中
driver.find_element_by_xpath("//a[translate(.,'-',' ') = name]").get_attribute('href')
阅读更多关于方法的信息
如果要处理:-按任意顺序编码>然后使用下面的xpath
//a[translate(.,'-;:',' ')='Ant Man and the Wasp']
屏幕截图: