【问题标题】:Python Selenium Vanguard: NoSuchElementException: no such element: Unable to locate element with Selenium and PythonPython Selenium Vanguard:NoSuchElementException:没有这样的元素:无法使用 Selenium 和 Python 定位元素
【发布时间】:2019-06-29 19:47:35
【问题描述】:

我正在尝试下载历史数据并单击指向历史数据的链接。但是,即使 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


    【解决方案1】:

    要单击元素,请等待页面加载并且元素可单击,然后单击。尝试整个 sn-p。

    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
    

    【讨论】:

      【解决方案2】:

      要点击文本为 搜索更多历史价格的元素,您需要诱导 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")))
        
      • 浏览器快照:

      【讨论】:

      • 这对我不起作用,我的目标是打开链接,以便我可以将密钥传递给日期,以便我可以将表格数据传递给美丽的汤。建议的代码似乎只是将窗口向下移动并且表格没有打开。使用检查时,我注意到“搜索更多历史价格”链接是“vuiNoBotBorder”类的一部分,这可能是个问题吗?
      猜你喜欢
      • 1970-01-01
      • 2022-11-14
      • 2021-07-07
      • 2019-10-25
      • 1970-01-01
      • 2021-10-21
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多