enter image description here The essence of what. There is a web page, I want to select the input field using selenium. First one, then the second. But they have the same class, the same divs. How to be in this case? I tried to specify which particular div I need with the help of the index [0] and 1 through xpath. But it does not work, it gives an error.

<div class="xdsoft_autocomplete" style="display: inline-block; width: 170px;"> <input data-id="" data-value="" type="text" class="nostyle str_obj xdsoft_input" value="" placeholder="Откуда?" style="font-size: 14.7px; background: transparent none repeat scroll 0% 0%;" autocomplete="off"><div 

There are two such "divas" and two of the same input, each within each diva. How to choose the first, then the second?

Attached the screen. Everything is stored in div id routers, they have two route typeaheaded diva, and it contains input that I need

My code is:

 addr_to = driver.find_element_by_xpath("//div[@id='routers']/input[contains(@class='nostyle str_obj xdsoft_input')]") 

It gives an error, even with the index [0], even without it. I can not understand what I am doing wrong

  • I would suggest finding_element_by_css_selector using pseudo-classes (choosing not indexes, but first-child or nth-of-type for example) ... IMHO, the perception of css selectors is much easier than xpath, respectively, easier to debug. - Bogdan
  • I think it's right that you use XPath, but you pile up))) - AtachiShadow
  • Can the second <input> be shown? - AtachiShadow

1 answer 1

You are in your XPath freaked out incredible sandwich, so also for non-existent data. Let's see what you pile up))):

  1. Your <div> element has no id attribute, but you try to find it by it:

<div class="xdsoft_autocomplete" style="display: inline-block; width: 170px;"> - there are class and style attributes, but you are looking for id - strongly!

  1. You use the contains(string, string) function contains(string, string) has two string variables, but declare only one in it - contains(@class='nostyle str_obj xdsoft_input') , and expect it to return something.

  2. Why use contains (str, str) if you can find an element by a pointer to an attribute - @ class = 'nostyle str_obj xdsoft_input' - such a pointer will find an element whose class is equal to the value nostyle str_obj xdsoft_input .

Depending on how you want to find your input element, you can sketch a couple of XPath variants that will be “free” to varying degrees:

  1. //*[@class="nostyle str_obj xdsoft_input"] - will find all elements whose class is equal to nostyle str_obj xdsoft_input
  2. //input[@class="nostyle str_obj xdsoft_input"] - the same as the top one, but only input elements will be searched
  3. //div/input[@class="nostyle str_obj xdsoft_input"] - the same as the top two, but now searches for the sequence div/input
  4. //div[@class="xdsoft_autocomplete"]/input[@class="nostyle str_obj xdsoft_input"] - the same as the top three, but now it will look for exact values ​​of the selected attributes ( class and class ) in the sequence div/input - This one is most similar to the one you wanted to write.