简体   繁体   English

选择正确的CssSelector或TagName

[英]Selecting the right CssSelector or TagName

Basic Information 基本信息
I have this software that I am developing for my personal use because im using it as a way to learn Selenium (By trial and error and so far I know the basics because I already went through it) 我拥有自己开发的供个人使用的软件,因为我将其用作学习硒的方式(通过反复试验,到目前为止,我已经了解了基础知识,因为我已经学习过它)

What the software does 该软件做什么
It will analyze the top 5 most active stocks from a list on the website and print it out on to the software (Pretty simple right?) 它将分析网站上列表中最活跃的5只股票,并将其打印到软件上(很简单吧?)

The Issue 问题

At this moment I've made it so it pulls a name & the percentage of top item from the list on this website (which used to be the top one) but then it changed to the 3rd one and what it does when I actually press the button is (as I just explained) it pulls the third option which is the one that used to be the first one 目前,我已经做到了,所以它从该网站的列表中拉出了名称和最高项目的百分比(以前是最高网站),但是后来变成了第三名,而当我实际按下时该按钮是(正如我刚才解释的那样),它拉​​出第三个选项,该选项曾经是第一个选项

What I WOULD like it to do 我想要它做什么

I would need the correct TagName or the CssSelector which shows whats in the list from 1-5 EVEN if it changes, I would like it to change when the website changes 我需要正确的TagName或CssSelector来显示1-5中列表中的内容,即使它发生更改,我希望它在网站更改时也能更改

Example

I press the button (label11_Click) (Sorry for the bad naming on my part) 我按下按钮(label11_Click)(对不起,我的名字不好)
It shows the top 5 items 它显示了前5个项目
I close the app and wait 24 hours 我关闭应用程序并等待24小时
The list changes on the website 列表在网站上更改
I reopen the application after 24 hours 我在24小时后重新打开应用程序
I press the same button (label11_Click) 我按下相同的按钮(label11_Click)
it shows the new top 5 items 它显示了新的前5个项目

private void label11_Click(object sender, EventArgs e)
{
    var getTopFive = new FirefoxDriver();
    getTopFive.Navigate().GoToUrl("https://www.tradingview.com/");

    IList<IWebElement> movies = getTopFive.FindElements(By.CssSelector("[data-symbol='NASDAQ:SIRI']")); 


    for (int i = 0; i < 1; ++i)
    {
        activeTextBox.Text = movies[i].Text;

    }

}

private void activePan_Paint(object sender, PaintEventArgs e)
{
    Label scan = new Label();
    scan.Text = "Test";
}

private void pictureBox2_Click(object sender, EventArgs e)
{
    var homePage = new FirefoxDriver();
    homePage.Navigate().GoToUrl("http://www.vargadevelopments.com");
}

private void label1_Click(object sender, EventArgs e)
{
    this.Close();
}

}

} }

List: https://embed.gyazo.com/75b4901627b3b7fc1736cc3d333a1995.png 列表: https//embed.gyazo.com/75b4901627b3b7fc1736cc3d333a1995.png
https://embed.gyazo.com/60e4766238cc6668805b4c53947dfff7.png https://embed.gyazo.com/60e4766238cc6668805b4c53947dfff7.png

It sounds like you want to select the table then get each the symbol name in the table. 听起来您想选择表,然后获取表中的每个符号名称。 You can do this using By.XPath and getAttribute . 您可以使用By.XPathgetAttribute进行此操作。 Something like below: 如下所示:

//Select the table rows that have the symbol names
var tableRows = getTopFive.FindElements(By.XPath("//*[@class='three-colomns-colomn active-quotes quote-tabbed-widget']//table/tbody/tr[@class='quote-ticker-inited']"));

//Loop through each row returned and get the full symbol name
foreach(var tableRow in tableRows )
{
    //Get the full symbol name (eg. NASDAQ:ABC)
    string fullSymbol = tableRow.getAttribute("data-symbol");

    //Separate the market and symbol name
    string market = fullSymbol.Substring(0,fullSymbol.indexof(":") - 1);
    string symbol = fullSymbol.Substring(fullSymbol.indexof(":") + 1);
}

A quick run-down of what the XPath is selecting: 快速了解XPath的选择:

  • //div[@class='three-colomns-colomn active-quotes quote-tabbed-widget'] selects the container of the most active table //div[@class='three-colomns-colomn active-quotes quote-tabbed-widget']选择最活跃表的容器
  • //table selects the table element that is an indirect child of the container //table选择table元素,该元素是容器的间接子级
  • /tbody selects the table body that is a direct child of the table /tbody选择作为表的直接子表的表主体
  • /tr[@class='quote-ticker-inited'] selects the table rows with the right class that are a direct child of the table body /tr[@class='quote-ticker-inited']选择具有正确类的表行,这些表行是表主体的直接子代

If you aren't familiar with XPath see here and here 如果您不熟悉XPath,请参见此处此处

I haven't tested the C# code but I did test the XPath query in Chrome and it selected the correct 5 table rows. 我没有测试C#代码,但是我确实在Chrome中测试了XPath查询,并且它选择了正确的5个表行。

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

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