引发 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的Chrome驱动程序使用Python登录到Applemusic 参考您可以在以下位置找到一些相关的讨论: 在iframe下处理#document的方式 通过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 timefrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysdriver = 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 ECConsole Output: $0.190You can find a detailed relevant discussion in How To sign in to Applemusic With Python Using Chrome Driver With SeleniumReferenceYou can find a couple of relevant discussions in:Ways to deal with #document under iframeSwitch to an iframe through Selenium and pythonselenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with seleniumselenium in python : NoSuchElementException: Message: no such element: Unable to locate element 这篇关于如何编写适当的Xpath来定位文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-26 20:27