A
A
Afrodita131242020-07-01 18:51:05
C++ / C#
Afrodita13124, 2020-07-01 18:51:05

How to properly write CssSelector using c# and OpenQA.Selenium.Chrome?

Getting familiar with UI automation. To begin with, I decided to write a simple C# code using OpenQA.Selenium.Chrome, which would drive a link to a chat into the Telegram search, select it and send a message. I was able to define a text field for searching and enter text into it, but I had problems with the remaining two steps.

Code in C#:

driver.Navigate().GoToUrl("https://web.telegram.org/#/login");

IWebElement query = driver.FindElement(By.CssSelector("input[type='search']"));
query.SendKeys("https://t.me/bAHdiTu");

IWebElement query1 = driver.FindElement(By.CssSelector("ul.nav.nav-pills.nav-stacked li:first-child"));
query1.Click();

IWebElement query2 = driver.FindElement(By.CssSelector("div[class='composer_rich_textarea']"));
query2.SendKeys("тест");


To select a chat, you need to select the first element from the ul - li:first-child. I tried to select it by class, but an exception is thrown: "OpenQA.Selenium.NoSuchElementException: "no such element: Unable to locate element: {"method":"css selector","selector":"ul.nav.nav-pills .nav-stacked li:first-child"}". I also tried to select the input field by class, but it didn't work.

HTML markup in Telegram WEB:
5efcb0c45ad6c314541557.png
5efcb0cde118b825898001.png
5efcb0d4f0cc1056657356.png
5efcb0dad1a5d091869172.png

Please help me describe the selector correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2020-07-03
@vabka

"input[type='search']"

"input[type=\"search\"]"
"ul.nav.nav-pills.nav-stacked li:first-child"

"ul.nav-pills.nav-stacked>li:first-child"
"div[class='composer_rich_textarea']"

"div.composer_rich_textarea"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question