简体   繁体   English

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

[英]C# Selenium navigate to Statistics on specifig Page

I am working with C# and Selenium.我正在使用 C# 和 Selenium。

On this Page i want to click on "Statistics": https://eatradingacademy.com/software/forex-historical-data/在此页面上,我想点击“统计”: 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.直到现在我才理解 XPath,但我正在研究它。

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

This is a special case because the element you want to click is located in an iFrame.这是一种特殊情况,因为您要单击的元素位于 iFrame 中。
To locate such element, you'll first have to switch the focus to the iFrame.要定位此类元素,您首先必须将焦点切换到 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.之后,您可以在 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();

Try below XPath试试下面的 XPath

在此处输入图像描述

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM