如何为软件找到正确的cssselector或标签 [英] How do I find the correct cssselector or tag for the software

查看:70
本文介绍了如何为软件找到正确的cssselector或标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本信息



我有这个软件,我正在为个人使用而开发,因为我正在使用它作为学习Selenium的一种方式(通过反复试验和迄今为止)我知道基础知识,因为我已经完成了它)



软件的功能



它将分析从网站列表中排名前5位最活跃的股票并将其打印到软件上(很简单吧?)



问题





此刻我已经做到了这一点,它取了一个名字&这个网站上列表中的顶级项目的百分比(曾经是最重要的一个),但随后它变为第三个,当我实际按下按钮时它的作用是(正如我刚才解释的那样)它拉出了第三个选项曾经是第一个的那个





我想做什么





我需要正确的TagName或CssSelector,它显示1-5列表中的最新情况如果它改变了,我希望它在网站改变时改变



示例





我按下按钮(label11_Click)(对不起我的命名不好)

它显示前5项

我关闭应用程序并等待24小时

网站上的列表更改

我24小时后重新打开申请

我按下相同的按钮(label11_Click)

它显示新的前5项







我的工作在momen t



网站: TradingView:免费股票图表和外汇图表在线。 [ ^ ]

列表:此列表正在网站上展示 [ ^ ]

检查员 [ ^ ]





我的代码



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?)

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

Example



I press the button (label11_Click) (Sorry for the bad naming on my part)
It shows the top 5 items
I close the app and wait 24 hours
The list changes on the website
I reopen the application after 24 hours
I press the same button (label11_Click)
it shows the new top 5 items



My Work At the moment


Website: TradingView: Free Stock Charts and Forex Charts Online.[^]
List: This List Is Being Showed On The Website[^]
The Inspector[^]


My Code

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;

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.PhantomJS;

namespace TestingUI
{
    public partial class mainForm : Form
    {

        
        Timer t = new Timer();


        public mainForm()
        {
            InitializeComponent();
        }

        int mouseX = 0, mouseY = 0;
        bool mouseDown;



             private void viewDash(object sender, MouseEventArgs e)
        {
            
        }


        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown = true;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseDown)
            {
                mouseX = MousePosition.X - 200;
                mouseY = MousePosition.Y - 40;

                this.SetDesktopLocation(mouseX, mouseY);

            }

        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            mouseDown = false;
        }

        private void leftsidePanel_Paint(object sender, PaintEventArgs e)
        {





        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Timer Interval
            t.Interval = 1000; //Milliseconds

            t.Tick += new EventHandler(this.t_Tick);

            //start timer when form loads
            t.Start();

        }


        //Timer eventhandeler

        private void t_Tick(object sender, EventArgs e)
        {
            //get the current time
            int hh = DateTime.Now.Hour;
            int mm = DateTime.Now.Minute;
            int ss = DateTime.Now.Second;


            //Time
            string time = "";

            //padding leading zero
            if (hh < 10)
            {
                time += "0" + hh;
            }

            else
            {
                time += hh;

            }
            time += ":";

            if (mm < 10)
            {
                time += "0" + mm;
            }
            else
            {
                time += mm;

            }
            time += ":";

            if (ss < 10)
            {
                time += "0" + ss;

            }
            else
            {
                time += ss;
            }
            //update label

            timeLabel.Text = time;
        }
 
        


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

        private void topPanel_Paint(object sender, PaintEventArgs e)
        {

        }


        private void dashPanel_MouseEnter_1(object sender, EventArgs e)
        {
            dashPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#1E1E1E");
        }

        private void dashPanel_MouseLeave(object sender, EventArgs e)
        {
            dashPanel.BackColor = Color.Transparent;
        }

        private void sideDashboard_MouseEnter(object sender, EventArgs e)
        {
            dashPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#1E1E1E");
        }

        private void mostActive_MouseEnter(object sender, EventArgs e)
        {
            dashActive.BackColor = System.Drawing.ColorTranslator.FromHtml("#1E1E1E");
        }

        private void mostActive_MouseLeave(object sender, EventArgs e)
        {
            dashActive.BackColor = Color.Transparent;
        }

        private void mostGainers_MouseEnter(object sender, EventArgs e)
        {
            dashGain.BackColor = System.Drawing.ColorTranslator.FromHtml("#1E1E1E");
        }

        private void mostGainers_MouseLeave(object sender, EventArgs e)
        {
            dashGain.BackColor = Color.Transparent;
        }

        private void mostLosers_MouseEnter(object sender, EventArgs e)
        {
            dashLose.BackColor = System.Drawing.ColorTranslator.FromHtml("#1E1E1E");
        }

        private void mostLosers_MouseLeave(object sender, EventArgs e)
        {
            dashLose.BackColor = Color.Transparent;
        }

        private void label2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void label4_MouseEnter(object sender, EventArgs e)
        {
            stocksPan.BackColor = System.Drawing.ColorTranslator.FromHtml("#9c9c9c");
        }

        private void label4_MouseLeave(object sender, EventArgs e)
        {
            stocksPan.BackColor = Color.Transparent;
        }

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

        private void label6_MouseEnter(object sender, EventArgs e)
        {
            stocksPanChrome.BackColor = System.Drawing.ColorTranslator.FromHtml("#9c9c9c");
        }

        private void label6_MouseLeave(object sender, EventArgs e)
        {
            stocksPanChrome.BackColor = Color.Transparent;
        }

        private void label6_Click(object sender, EventArgs e)
        {
            //var chromeDriver = new ChromeDriver();
            //chromeDriver.Navigate().GoToUrl("https://www.tradingview.com/");
        }

        private void stockList_TextChanged(object sender, EventArgs e)
        {
        }


        private void sideDashboard_Click(object sender, EventArgs e)
        {
            activePan.Visible = false;
            gainpan.Visible = false;
            losPan.Visible = false;
        }

        private void mostActive_Click(object sender, EventArgs e)
        {
            activePan.Visible = true;
            gainpan.Visible = false;

        }

        private void mostGainers_Click(object sender, EventArgs e)
        {
            activePan.Visible = true;
            gainpan.Visible = true;
            losPan.Visible = false;
        }

        private void mostLosers_Click(object sender, EventArgs e)
        {
            activePan.Visible = true;
            gainpan.Visible = true;
            losPan.Visible = true;
        }

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

//This used to be the first one
            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();
        }

        }
    }





@SAKyrukov

SAKryukov



我尝试过:



我是尝试更改CssSelector,但它或者给了我一个错误或根本没有做任何事情,我认为我使用错误的方式。



@SAKyrukov
SAKryukov

What I have tried:

I've tried changing the CssSelector but it either gave me an error or didnt do anything at all, I think im using them the wrong way.

推荐答案

当试图使用selenium时掌握Xpath非常重要。如果您不是使用css选择器搜索xpath,您可以尝试这样的事情:

// div [@ id ='market-summary-body'] / div [2] / div [@ class ='pages'] / div / div / table / tbody / tr

我在谷歌浏览器中测试的将为您提供所有顶级活动行的列表。
When trying to use selenium it is important to get a grasp on Xpath. If instead of using the css selector you search by xpath you can try something like this:
//div[@id='market-summary-body']/div[2]/div[@class='pages']/div/div/table/tbody/tr
Which I tested in google chrome will get you a list of all the top active rows.


这篇关于如何为软件找到正确的cssselector或标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆