简体   繁体   English

如何编写适当的 Xpath 来定位文本值

[英]How to write appropriate Xpath to locate text value

I am trying to use python, selenium and xpath to grab the text $0.180 in the following block of HTML,我正在尝试使用 python、selenium 和 xpath 在 Z4C4AD5FCA2E38A1CAED7 的以下块中获取文本 $0.180

<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:此代码深入到网站 HTML 中,我尝试使用以下 Xpath 来定位它:

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任何帮助将不胜感激

<iframe class="wl-summary" id="wl-summary" src="https://cloud.weblink.com.au/clients/smallcaps/summary/summary.aspx?symbol=AWJ&amp;" width="100%" height="25" frameborder="0" scrolling="no" allowtransparency="true" style="height: 1077px;"></iframe>
<iframe class="wl-quote-frame" src="quoteFrame.aspx?symbol=AWJ&amp;" scrolling="no" frameborder="0" height="1" style="height: 177px;"></iframe>

It's in a double nested iframe so you have to switch to it.它位于双嵌套 iframe 中,因此您必须切换到它。 This also has the appropriate waits instead of using time.sleep() and hoping the elements load.这也有适当的等待,而不是使用 time.sleep() 并希望元素加载。 It also clicks the popup to get out of the way and prints the spans text.它还单击弹出窗口以避开并打印跨度文本。

wait = WebDriverWait(driver, 10)
driver.get("https://smallcaps.com.au/stocks/?symbol=AWJ")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#tve_editor > div'))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, 'wl-summary')))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, 'wl-quote-frame')))
elem=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'span.wlp-last-price-span')))
print(elem.text)

Import进口

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

The last price field is within nested <iframe> elements so you have to:最后一个价格字段位于嵌套的<iframe>元素中,因此您必须:

  • Induce WebDriverWait for the parent frame to be available and switch to it .诱导WebDriverWait使框架可用并切换到它

  • Induce WebDriverWait for the child frame to be available and switch to it .诱导WebDriverWait使框架可用并切换到它

  • Induce WebDriverWait for the desired element to be visible .诱导WebDriverWait使所需元素可见

  • You can use either of the following Locator Strategies :您可以使用以下任一定位器策略

    • Using CSS_SELECTOR :使用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 :使用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:控制台 Output:

     $0.190

You can find a detailed relevant discussion in How To sign in to Applemusic With Python Using Chrome Driver With Selenium您可以在How To sign in to Applemusic With Python Using Chrome Driver With Selenium中找到详细的相关讨论


Reference参考

You can find a couple of relevant discussions in:您可以在以下位置找到一些相关的讨论:

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

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