Aruba
Aruba

Reputation: 17

Finding CSS selector path for Selenium C#

I am new to Selenium C# automation. Tried finding on web but did not get any help. The html code looks like this. I need to find the element and then click it using CSS. The site only runs on IE.

    <tbody>
    <tr class="t-state-selected">
    <td>Purchased</td>
    <td class="">768990192</td>

Upvotes: 0

Views: 6193

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193068

To click on the number 768990192 which is dynamic we have to construct a CssSelector as follows :

driver.FindElement(By.CssSelector("tr.t-state-selected td:nth-of-type(2)")).Click();

Upvotes: 1

Josh
Josh

Reputation: 4438

I know web links can disappear, but here are a few I use when trying to figure out how to locate elements using Selenium's C# WebDriver:

https://automatetheplanet.com/selenium-webdriver-locators-cheat-sheet/

https://saucelabs.com/resources/articles/selenium-tips-css-selectors

https://www.packtpub.com/mapt/book/web_development/9781849515740/1

The bottom line is that you're selecting by id, class, or XPath. Each of these can be tested directly on the page using the F12 browser tools. For example, to find the first comment on your question above, you could try this in the console:

$x("//div[@id='mainbar']//tbody[@class='js-comments-list']/tr")

Here's another SO post with a quick and dirty answer.

And here is the official documentation from Selenium on how to locate UI elements.

Upvotes: 1

Kent Kostelac
Kent Kostelac

Reputation: 2446

You're really not giving us much info to work. I will try my best to accommodate. Even though the presented HTML is not enough to give an indication of the format and you've not presented any code of your current solution.

string url = "https://www.google.com";
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl(url);
driver.FindElement(By.XPath("//tr[@class='t-state-selected']")).Click();

This little code snippet.

Creates a internet explorer driver.

Goes to the url of your choice.

And then clicks the table row that has a class that equals "t-state-selected'. Which my guess is all or none of the table rows.

Upvotes: 0

Related Questions