如何编写适当的Xpath来定位文本值 [英] How to write appropriate Xpath to locate text value
问题描述
我正尝试使用python,selenium和xpath在以下HTML块中抓取$ 0.180的文本,
< div class ="wlp-values-div">< span class ="wlp-last-price-span"> $ 0.180</span>
此代码深入到网站HTML中,我尝试使用以下Xpath进行定位:
导入时间从硒导入webdriver从selenium.webdriver.common.keys导入密钥驱动程序= webdriver.Chrome()driver.get("https://smallcaps.com.au/stocks/?symbol=AWJ")time.sleep(3)webdriver.ActionChains(驱动程序).send_keys(Keys.ESCAPE).perform()ele = driver.find_element_by_xpath("//div [@ class ='wlp-values-div']/span [@ class ='wlp-last-price-span']")
但是我收到错误消息:
NoSuchElementException:消息:没有这样的元素:无法找到元素:{方法":"xpath",选择器":"//div [@ class ='wlp-values-div']/span [@ class ='wlp-last-price-span']'}
任何帮助将不胜感激
最后价格字段位于嵌套的< iframe>
元素内,因此您必须:>
-
引发 WebDriverWait 使父级 框架可用并切换到.
-
引发 WebDriverWait 为子 框架可用并切换到.
-
引发 WebDriverWait 使所需的元素可见.
-
您可以使用以下定位器策略:
-
使用
CSS_SELECTOR
:driver.get('https://smallcaps.com.au/stocks/?symbol=AWJ')WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,``svg [data-name ='close']'''')))).click()WebDriverWait(驱动程序,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,``iframe#wl-summary''))))WebDriverWait(驱动程序,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.wl-quote-frame")))print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"span.wlp-last-price-span"))).get_attribute("innerHTML"))driver.quit()
-
使用
XPATH
:driver.get('https://smallcaps.com.au/stocks/?symbol=AWJ')WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,``//* [name()='svg'and @ data-name ='close']'''''))).click()WebDriverWait(驱动程序,20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe [@ id ='wl-summary']")))WebDriverWait(驱动程序,20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe [@ class ='wl-quote-frame']"))))print(WebDriverWait(驱动程序,20).until(EC.visibility_of_element_located((By.XPATH,"//span[@class='wlp-last-price-span']")))).get_attribute("innerHTML";))driver.quit()
-
-
注意:您必须添加以下导入:
从selenium.webdriver.support.ui中的导入WebDriverWait来自selenium.webdriver.common.by导入方式从selenium.webdriver.support导入EC的预期条件
-
控制台输出:
$ 0.190
通过Selenium和python切换到iframe selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:尝试单击带有硒的下一步按钮时找不到元素 Python中的硒:NoSuchElementException:消息:没有这样的元素:无法找到元素 I am trying to use python, selenium and xpath to grab the text $0.180 in the following block of HTML,
<div class="wlp-values-div"> <span class="wlp-last-price-span">$0.180</span>
This code is deep into a websites HTML and I tried to use the following Xpath to locate it:
import time from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome() driver.get("https://smallcaps.com.au/stocks/?symbol=AWJ") time.sleep(3) webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform() ele = driver.find_element_by_xpath("//div[@class='wlp-values-div']/span[@class='wlp-last-price-span']")
But I receive the error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='wlp-values-div']/span[@class='wlp-last-price-span']"}
Any help would be greatly appreciated
解决方案The last price field is within nested
<iframe>
elements so you have to:
Induce WebDriverWait for the parent frame to be available and switch to it.
Induce WebDriverWait for the child frame to be available and switch to it.
Induce WebDriverWait for the desired element to be visible.
You can use either of the following Locator Strategies:
Using
CSS_SELECTOR
:driver.get('https://smallcaps.com.au/stocks/?symbol=AWJ') WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "svg[data-name='close']"))).click() WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#wl-summary"))) WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.wl-quote-frame"))) print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.wlp-last-price-span"))).get_attribute("innerHTML")) driver.quit()
Using
XPATH
:driver.get('https://smallcaps.com.au/stocks/?symbol=AWJ') WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg' and @data-name='close']"))).click() WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='wl-summary']"))) WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='wl-quote-frame']"))) print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='wlp-last-price-span']"))).get_attribute("innerHTML")) driver.quit()
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
Console Output:
$0.190
You can find a detailed relevant discussion in How To sign in to Applemusic With Python Using Chrome Driver With Selenium
Reference
You can find a couple of relevant discussions in:
- Ways to deal with #document under iframe
- Switch to an iframe through Selenium and python
- selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
- selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
这篇关于如何编写适当的Xpath来定位文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!