使用VB.NET的Selenium - 找不到元素

时间:2016-11-25 11:11:50

标签: css vb.net selenium xpath

我是Selenium的新手,我偶然发现了一个错误。

我正在尝试从网站下载一个特定文件(见代码),当我到达下载链接可用的网站的最后一部分时,我无法选择“下载完整证券列表” ”。我尝试过使用XPath CSS选择器等等。

你们这些技术娴熟的人可以提出修复/推荐吗?

任何帮助都会很棒!

谢谢你, 吉汗

Sub downloadDBX()
    'Initalise Chrome Browser For Execution
    Dim driver As IWebDriver
    driver = New ChromeDriver("C:\Users\cihan\Documents\Selenium")

    'Navigate to the DBx Website
    driver.Navigate().GoToUrl("https://etf.deutscheam.com/GBR/ENG/Disclaimer/Institutional")

    'Get ElementID to navigate DBX Website
    Dim agreeTerms As IWebElement = driver.FindElement(By.XPath("//*[@id='form']/div[3]/div[2]/div[2]/div/div/div[1]/div/div/div/div[2]/div[3]/div[2]/div/div[2]/div[1]/a"))
    System.Threading.Thread.Sleep(1000)
    agreeTerms.Click()

    'Navigate to the equities section of DBx
    Dim equitiesPage As IWebElement = driver.FindElement(By.XPath("//*[@id='form']/div[3]/div[2]/div[3]/span[1]/div/div/div[2]/div[3]/div/div[2]/div/ul/li[2]/a"))
    System.Threading.Thread.Sleep(1000)
    equitiesPage.Click()

    'click relevant fund
    Dim myFundInformation As IWebElement = driver.FindElement(By.PartialLinkText("ATX UCITS ETF (DR)"))
    System.Threading.Thread.Sleep(1000)
    myFundInformation.Click()

    'change to "ETF Information tab" to gather required data
    Dim myFundHoldings As IWebElement = driver.FindElement(By.XPath("//*[@id='ctl02_ctl15_ctl00_ctl01_126']/span"))
    System.Threading.Thread.Sleep(1000)
    myFundHoldings.Click()


    'download my holdings
    Dim myData As IWebElement = driver.FindElement(By.XPath("//*[@id='ctl02_ctl15_ctl00_container']div[2]/div[3]/div/div[4]/div[2]/div/a/div[2]/span"))
    System.Threading.Thread.Sleep(1000)
    myData.Click()
End Sub

1 个答案:

答案 0 :(得分:0)

这是通过搜索链接文本而不是下载链接的XPATH来解决的。之前和之后如下:

'download my holdings
Dim myData As IWebElement = driver.FindElement(By.XPath("//*[@id='ctl02_ctl15_ctl00_container']div[2]/div[3]/div/div[4]/div[2]/div/a/div[2]/span"))
System.Threading.Thread.Sleep(1000)
myData.Click()

现在:

    'download my holdings
    Dim myData As IWebElement = driver.FindElement(By.LinkText("Download Full Securities List"))
    System.Threading.Thread.Sleep(1000)
    myData.Click()