简体   繁体   中英

How to use findElement(By.cssSelector("")) in Visual Studio in Selenium C#

I'm trying to use

driver.findElement(By.cssSelector("....."));

But it doesn't work(cannot use the FindElement with By). Do you know what can replace it? I'm using visual studio 2019 and write in C#

using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
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;


namespace WebDriverTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            var driver = new ChromeDriver();
            driver.Navigate().GoToUrl("http://www.marketinginternetdirectory.com/submit.php");

            //Find form element to auto fill on form page - Right click on input->inspect
            var title_field = driver.FindElement(By.)

        }
    }
}

You can use xpath https://www.w3schools.com/xml/xpath_intro.asp

For your specific example you can locate title by doing this:

var titleElement = driver.FindElement(By.XPath("//input[@name = 'TITLE']"));

// And to go further you might after want to do something like this:
element.SendKeys("YOUR-TITLE");

As per the documentation in By Class the notations is as follows:

  • CssSelector : Gets a mechanism to find elements by their cascading style sheet (CSS) selector.

Your effective line of code will be:

using OpenQA.Selenium;

var title_field = driver.FindElement(By.CssSelector("svg g rect:nth-child(20)"));

Reference

Selenium C# Webdriver Tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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