蟒硒先锋:NoSuchElementException异常:没有这样的元件:无法与硒和Python定位元件

问题描述 投票:1回答:2

我试图下载历史数据,然后单击链接到的历史数据。然而,即使Xcode是正确的我得到这个错误:

NoSuchElementException: no such element: Unable to locate element.

代码试验:

from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
driver = webdriver.Chrome(executable_path='/Users/Documents/Coding/chromedriver')
url = "https://www.vanguardinvestor.co.uk/investments/vanguard-lifestrategy-100-equity-fund-accumulation-shares/price-performance?intcmpgn=blendedlifestrategy_lifestrategy100equityfund_fund_link"
driver.get(url)
wait = WebDriverWait(driver, 10)
elem = driver.find_element_by_xpath("//*[@id='prices-and-performance-tab']/div/div[4]/div[3]/div[1]/div[1]/div[3]/div/div/div[2]/div/table/tfoot/tr/td/a")
webdriver.ActionChains(driver).move_to_element(elem).click(elem).perform()
python-3.x selenium selenium-webdriver webdriver webdriverwait
2个回答
0
投票

点击元素上等待页面加载和元素,可以点击,然后click.Try整个片段。

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

driver = webdriver.Chrome(executable_path='/Users/Documents/Coding/chromedriver')
url = "https://www.vanguardinvestor.co.uk/investments/vanguard-lifestrategy-100-equity-fund-accumulation-shares/price-performance?intcmpgn=blendedlifestrategy_lifestrategy100equityfund_fund_link"
driver.get(url)
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()[contains(.,'Price & Performance')]]")))
element.click

0
投票

点击与文本搜索您需要诱导WebDriverWait的元素是可以点击的,您可以使用以下解决方案的更多历史的元素:

  • 代码块: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC options = webdriver.ChromeOptions() options.add_argument("--start-maximized") options.add_argument("--disable-extensions") driver = webdriver.Chrome(chrome_options=options, executable_path = r'C:\Utility\BrowserDrivers\chromedriver.exe' ) driver.get("https://www.vanguardinvestor.co.uk/investments/vanguard-lifestrategy-100-equity-fund-accumulation-shares/price-performance?intcmpgn=blendedlifestrategy_lifestrategy100equityfund_fund_link") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='bannerButton']"))).click() more_historical_prices = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.LINK_TEXT, "Search for more historical prices"))) driver.execute_script("arguments[0].scrollIntoView(true);", more_historical_prices) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Search for more historical prices")))
  • 浏览器快照:

more_historical_prices

© www.soinside.com 2019 - 2024. All rights reserved.