简体   繁体   中英

How to find a specific html element with python selenium

So I have this element in html:

<input data-v-72dea36a="" type="text" name="tradelink" placeholder="Enter your code" style="margin-right: 25px;">

How can I select exactly this one? I tried:

find_element_by_name('tradelink')

There is another element before this one with the same name...it doesn't have an id or class name...

If there are just 2 elements with attribute name="tradelink" , and assuming you need the seconds one, you can use:

find_elements_by_name('tradelink')[1]

Another way is using xpath to match the placeholder value:

find_element_by_xpath("//input[@placeholder='Enter your code']")

Notes:

  • Notice the plural on element S on the 1st example
  • Selenium Docs - Locating Elements
  • Fore more than 2 elements, you may have to change the array item number, ie: find_elements_by_name('tradelink')[2]

Rightclick the element within the inspect element editor and select copy full xpath. As posted above, paste that code into the parentheses in find_element_by_xpath()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM