如何通过Selenium和Python使用xpath精确计算数字
您好,我正在尝试查找包含文本“01.0000”的元素,但此行前有一个包含“01.00”的源代码。如何定位此元素 源代码:如何通过Selenium和Python使用xpath精确计算数字,python,selenium,selenium-webdriver,xpath,webdriver,Python,Selenium,Selenium Webdriver,Xpath,Webdriver,您好,我正在尝试查找包含文本“01.0000”的元素,但此行前有一个包含“01.00”的源代码。如何定位此元素 源代码: <div class="gofer-search-results"> <a href="#" class="js-hovered"><span class="name">Communication and Media Studies</span><span class="code">09.01</span>
<div class="gofer-search-results">
<a href="#" class="js-hovered"><span class="name">Communication and Media Studies</span><span class="code">09.01</span></a>
<a href="#" class=""><span class="name">Communication, General</span><span class="code">09.0100</span></a>
<a href="#" class=""><span class="name">Speech Communication and Rhetoric</span><span class="code">09.0101</span></a>
<a href="#" class=""><span class="name">Mass Communication/Media Studies</span><span class="code">09.0102</span></a>
<a href="#" class=""><span class="name">Communication and Media Studies, Other</span><span class="code">09.0199</span></a>
<a href="#" class=""><span class="name">Animal Sciences</span><span class="code">01.09</span></a></div>
我一直使用01.00元素而不是01.0000元素。要使用文本定位此元素为01.0000,可以使用以下任一解决方案:
- 使用text():
- 使用normalize-space():
[x代表驱动程序中的x。如果x.text='09.0100']
我忘了输入',请通过xpath(“//div/a/span”)查找元素。谢谢!
driver.find_element_by_xpath("//span/div/a/span[text()= 01.0000]")
driver.find_element_by_xpath("//span/div/a/span[normalize-space()= 01.0000]")
driver.find_element_by_xpath("//span/div/a/span[./text()= 01.0000]")
driver.find_element_by_xpath("//span/div/a/span[text()='01.0000']")
driver.find_element_by_xpath("//span/div/a/span[normalize-space()='01.0000']")