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

Time:07-29

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

        }
    }
}

CodePudding user response:

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 if you want to go a step further you might after want to do something like this:
element.SendKeys("YOUR-TITLE");

CodePudding user response:

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

  • Related