0

I am working with C# and Selenium.

On this Page i want to click on "Statistics": https://eatradingacademy.com/software/forex-historical-data/

I tried out to click with several modified codes. Fairly try & error. I dont understand XPath until now, but i am working on it.

Can someone here help me out? What lines of Code are needed to click this 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

Edit: Code added:

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();
            }
        }

    }

}
Dragon
  • 49
  • 7

2 Answers2

1

This is a special case because the element you want to click is located in an iFrame.
To locate such element, you'll first have to switch the focus to the iFrame.
You can do this like this:

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

After that you can locate the element within the iFrame. I would suggest taking a combination of class and text in this case because if you only search for the text, another element which contains just the word 'Settings' or 'Statistics' may be located instead.
This would result in following line:

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

Update: I think the problem may be the element is not in your viewport. I changed the code to scroll to the element before trying to click it.
Try this:

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();
Ruben
  • 366
  • 3
  • 8
  • I understand your idea and can read your Code. Thats totally fine for me. Completely new for me was your AND statement inside XPath. Pretty god to know. After adding this two lines of Code in my project, nothing new happens. No error came up, but also the link was not clicked. There must be something more ... But always thank you for your try. – Dragon May 24 '22 at 17:08
  • Can you provide the full code you are using? This implementation works on my machine. – Ruben May 24 '22 at 17:13
  • I added my code in the first Thread. My only changes there was: I cleaned it up from not needed trash before... You will see, i am really in my first steps with all of this around^^ But i want to get my ideas work and want to understand why ... for my future projects. Thank you – Dragon May 24 '22 at 17:41
  • I updated my answer again, i hope it works. Make sure you click the notification first. It may intercept the click on 'Statistics' – Ruben May 24 '22 at 18:03
  • I tried your updated code. Now VisualStudio give me an Error and "Actions" is marked red. The error is called something like: Type or Namespace Actions not found. Is there something to add in the top : using blabla? Or something else like this? – Dragon May 24 '22 at 18:28
  • In Visual Studio, you can hover over the redline and click the lightbulb thingy. You can create imports automatically like that. If that doesn't work for you just add: using OpenQA.Selenium.Interactions; – Ruben May 24 '22 at 18:32
  • I found it. Thank you. Code changed to: OpenQA.Selenium.Interactions.Actions actions = new OpenQA.Selenium.Interactions.Actions(webdriver); In the end i got this Error from Studio: "OpenQA.Selenium.ElementClickInterceptedException" in WebDriver.dll – Dragon May 24 '22 at 18:37
  • Well this code works perfectly on my machine.. Are you sure you deny the popup message first and nothing is blocking the statistics button? – Ruben May 24 '22 at 18:40
  • I will use this driver: ChromeDriver 101.0.4951.41 chromedriver_win32.zip for Chome latest updated: Version 101.0.4951.67 (Offizieller Build) (64-Bit) – Dragon May 24 '22 at 18:40
  • Oh my goodness ... Thanks for requesting again ! Thats it. I missed to deny the popup message first. SHAME ON ME ;) It is working now. Many thanks to you ... – Dragon May 24 '22 at 18:47
  • No worries, everyone starts somewhere. Glad i could help :) – Ruben May 24 '22 at 18:48
0

Try below XPath

enter image description here

//a[translate(normalize-space(.), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='statistics']
Naeem Ahmed
  • 216
  • 1
  • 6