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