繁体   English   中英

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

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

我正在使用 C# 和 Selenium。

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

我尝试点击几个修改后的代码。 相当尝试和错误。 直到现在我才理解 XPath,但我正在研究它。

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

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

页码

编辑:添加代码:

 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 中找到该元素。 在这种情况下,我建议结合使用类和文本,因为如果您只搜索文本,则可能会找到另一个仅包含“设置”或“统计”一词的元素。
这将导致以下行:

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']

暂无
暂无

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

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