Home > Enterprise >  Why selenium finds the same element at list which has deferents parents
Why selenium finds the same element at list which has deferents parents

Time:12-22

I can't understand why it returns me the same element? If I just print the outerHTML of each element in els, it shows that all of them different. But when I run code below, it shows the same.

link = 'https://www.tme.eu/ru/katalog/vykliuchateli-kontsevye_100405/?s_field=1000012&s_order=asc&limit=20&currency=EUR&visible_params=2,2193,2019,1390,938,936,82,32,42,49,1391,20x5,694,725,695,1395,13,1385,11,1605,2390,412,45,413,708,157,757,707,98,1386,693,117,193,22,23,2412,1393,699,698,436,2556&mapped_params=2:131;'

import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By

d = webdriver.Chrome('chromedriver-2')
d.get(link)

els = d.find_elements(By.XPATH, '//*[@]')
for q in els:
    el = q.find_element(By.XPATH, '//*[@]')
    print(el.get_attribute('outerHTML'))

Output is:

DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  d = webdriver.Chrome('chromedriver-2')
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>
<h4 >MAM1F11Z11</h4>

CodePudding user response:

You are getting the same element all the times because

q.find_element(By.XPATH, '//*[@]')

finds the first element matching the //*[@] XPath locator starting from the beginning of the page.
To find element inside (below) specific element you shoud add a dot . in the front of the XPath

q.find_element(By.XPATH, './/*[@]')

CodePudding user response:

You are not seeing the same element but different elements but the 20elements

  • Related