C# Selenium 导航到特定页面上的统计信息

C# Selenium navigate to Statistics on specifig Page

我正在使用 C# 和 Selenium。

在此页面上,我想单击“统计信息”: https://eatradingacademy.com/software/forex-historical-data/

我试着用几个修改过的代码点击。公平地尝试和错误。 直到现在我还不了解 XPath,但我正在研究它。

这里有人可以帮我吗? 单击此 Link 需要哪些代码行?

//webdriver.FindElement(By.XPath("//a[text()='Settings']")).Click();
//webdriver.FindElement(By.XPath("//li[@class='nav-item']/a[text()='Settings']")).Click();
// webdriver.FindElement(By.XPath("//li/a/span[.='Settings']")).Click();
//webdriver.FindElement(By.XPath("//a[contains(.,'Settings')]")).Click();
//webdriver.FindElement(By.XPath("//a[contains(text(),'Settings')]")).Click();

Code of Page

编辑:添加代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

// Selenium added
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Threading;

using System.Runtime.InteropServices;
using System.IO;


namespace Historical_Data_Manager
{

    public partial class frm_Historical_Data_Manager : Form
    {

        ChromeDriver webdriver; 
        Thread th;

        public frm_Historical_Data_Manager()
        {
            InitializeComponent();
        }

        private void frm_Historical_Data_Manager_Load(object sender, EventArgs e)
        {

        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            StartDriverChrome();
            NavigateToURL();
            NavigateToSettings();
        }

        private void NavigateToSettings()
        {
            try
            {
                // Benachrichtigungs-Abfrage Wegklicken --NOT YET-- "Would you like to receive notifications on latest updates?"
                //webdriver.FindElement(By.Id("webpushr-deny-button")).Click();
                // This deactivates the Request on page


                //webdriver.SwitchTo().Frame(webdriver.FindElement(By.Id("data-app-frame")));
                //webdriver.FindElement(By.XPath("//a[@class='nav-link panel-switch' and contains(text(), 'Statistics')]")).Click();


                //webdriver.FindElement(By.XPath("//a[translate(normalize-space(.), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='statistics']")).Click();

                //a[translate(normalize-space(.), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='statistics']

                // Benachrichtigungs-Abfrage Wegklicken --NOT YET-- "Would you like to receive notifications on latest updates?"
                //webdriver.FindElement(By.Id("webpushr-deny-button")).Click();



                // Navigiere zu den Settings
                //webdriver.FindElement(By.XPath("//a[text()='Settings']")).Click();
                //webdriver.FindElement(By.XPath("//li[@class='nav-item']/a[text()='Settings']")).Click();
                // webdriver.FindElement(By.XPath("//li/a/span[.='Settings']")).Click();
                //webdriver.FindElement(By.XPath("//a[contains(.,'Settings')]")).Click();
                //webdriver.FindElement(By.XPath("//a[contains(text(),'Settings')]")).Click();


                // //webdriver.FindElement(By.XPath("")).Click();

                //webdriver.SwitchTo().Frame("data-app-frame");
                //webdriver.FindElement(By.XPath("//a[text()='Settings']")).Click();
                //webdriver.FindElement(By.XPath("//a[text()='Settings']")).Click();

            }
            catch
            {
                // Programm beenden ...
                MessageBox.Show("Error ....." + Environment.NewLine + "bla bla", "Fehler: blub",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error
                );
                CloseApplication();
            }
        }

        private void StartDriverChrome()
        {
            try
            {
                // Selenium Driver starten:
                ChromeDriverService service = ChromeDriverService.CreateDefaultService();
                service.HideCommandPromptWindow = false; // hide Console
                webdriver = new ChromeDriver(service);
            }
            catch
            {
                // Programm beenden ...
                MessageBox.Show("Der Chrome-Driver konnte nicht gestartet werden." + Environment.NewLine + "Eventuell fehlt die Datei ---chromedriver.exe---","Fehler: Webdriver nicht gestartet",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error // for Error 
                                                     //MessageBoxIcon.Warning // for Warning  
                                                     //MessageBoxIcon.Information  // for Information
                                                     //MessageBoxIcon.Question // for Question
                );
                CloseApplication();
            }
        }

        private void NavigateToURL()
        {
            try
            {
                webdriver.Navigate().GoToUrl("https://eatradingacademy.com/software/forex-historical-data/");
                Thread.Sleep(1000);
            }
            catch
            {
                // Programm beenden ...
                MessageBox.Show("Die Webseite konnte nicht aufgerufen werden.", "Fehler: Webseite nicht gefunden",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error // for Error 
                                                     //MessageBoxIcon.Warning // for Warning  
                                                     //MessageBoxIcon.Information  // for Information
                                                     //MessageBoxIcon.Question // for Question
                );
                CloseApplication();
            }
        }



        private void CloseWebdriver()
        {
            try
            {
                webdriver.Quit();
            }
            catch
            {
                // do nothing
            }
        }

        private void CloseApplication()
        {
            try
            {
                Application.Exit();
            }
            catch
            {
                // do nothing
            }
        }

        private void frm_Historical_Data_Manager_Closed(object sender, FormClosedEventArgs e)
        {
                CloseWebdriver();
                CloseApplication();
        }

        private void btn_Stop_Click(object sender, EventArgs e)
        {
            CloseWebdriver();
            CloseApplication();
        }

        private void FileNotExist(string Filename)
        {
            //Ergebnis: C:\Program Files\MyApplication\MyApplication.exe
            //string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;

            //Ergebnis: C:\Program Files\MyApplication
            //string strWorkPath = System.IO.Path.GetDirectoryName(strExeFilePath)

            //Ergebnis: C:\Program Files\MyApplication\Settings.xml
            //string strSettingsXmlFilePath = System.IO.Path.Combine(strWorkPath, "Settings.xml");

            string strApplication_FilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string strApplication_Path = System.IO.Path.GetDirectoryName(strApplication_FilePath);

            string strINIFilePath = System.IO.Path.Combine(strApplication_Path, Filename);
            if (!File.Exists(strINIFilePath))
            {
                MessageBox.Show("Die Datei " + Filename + " konnte nicht gefunden werden!" + Environment.NewLine + "Das Programm wird beendet.",
                                "Fehler: Datei nicht gefunden",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error // for Error 
                                                     //MessageBoxIcon.Warning // for Warning  
                                                     //MessageBoxIcon.Information  // for Information
                                                     //MessageBoxIcon.Question // for Question
                );
                Application.Exit();
            }
        }

    }

}

这是一种特殊情况,因为您要单击的元素位于 iFrame 中。
要定位此类元素,您首先必须将焦点切换到 iFrame。
你可以这样做:

webdriver.SwitchTo().Frame(webdriver.findElement(By.id("data-app-frame"));

之后,您可以在 iFrame 中定位该元素。在这种情况下,我建议结合使用 class 和文本,因为如果您只搜索文本,则可能会找到仅包含单词 'Settings' 或 'Statistics' 的另一个元素。
这将导致以下行:

webdriver.FindElement(By.XPath("//a[@class='nav-link panel-switch' and contains(text(), 'Statistics')]")).Click();

更新:我认为问题可能是该元素不在您的视口中。我更改了代码以在尝试单击之前滚动到该元素。
试试这个:

webdriver.SwitchTo().Frame(webdriver.FindElement(By.Id("data-app-frame")));
var statistics = webdriver.FindElement(By.XPath("//a[@class='nav-link panel-switch' and contains(text(), 'Statistics')]"));
Actions actions = new Actions(webdriver);
actions.MoveToElement(statistics);
actions.Perform();
statistics.Click();

尝试下面的 XPath

//a[translate(normalize-space(.), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='statistics']