K
K
kykyryky2016-09-22 11:45:21
Programming
kykyryky, 2016-09-22 11:45:21

How to find an element on a web page?

The page has radio buttons with city names. The history of city selection is displayed in a separate block on the right.
I select the element I need like this:

IWebDriver driver = new ChromeDriver();
IWebElement element = driver.FindElement(By.XPath("//*[contains(text(), '" + CityName + "')]"));
element.Click();

Everything is fine here, finds and clicks. But sometimes you need to click on the city again, and selenium selects not the desired radio button, but that right block.
I understand that the problem is in the XPath expression. How can I fix the expression so that exactly the buttons I need are selected unambiguously?
For example, all my radio buttons are in a div with the class "div1". Inside this div, there can be more nested elements, such as lists for separating buttons into columns, and so on.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2016-09-22
@kykyryky

Just add some specifics to the xpath (assuming div1 is the only class):
It's not the most elegant way, but it should work. There may be several options to solve the problem, but you need to look at the html code of the page for this.

S
sim3x, 2016-09-22
@sim3x

Chrome
f12 Inspect
element
mouse right click on element
In menu: Copy -> Copy XPath

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question