简体   繁体   中英

How to get parents of an element with xpath in selenium (python)

I have a script that takes all the images I want on a webpage, then I have to take the link that enclose the image .

I actually click on every image, take the current page link and then I go back and continue with the work. This is slow but I have an a tag that "hug" my image , I don't know how to retrieve that tag. With the tag it could be easier and faster. I attach the html code and my python code!

HTML code

<div class="col-xl col-lg col-md-4 col-sm-6 col-6">
<a href="URL I WANT TO GET ">
<article>
<span class="year">2017</span>
<span class="quality">4K</span>
<span class="imdb">6.7</span>
<img width="190" height="279" src="THE IMAGE URL" class="img-full wp-post-image" alt="" loading="lazy"> <h2>TITLE</h2>
</article>
</a></div>
<div class="col-xl col-lg col-md-4 col-sm-6 col-6">
<a href="URL I WANT TO GET 2">
<article>
<span class="year">2019</span>
<span class="quality">4K</span>
<span class="imdb">8.0</span>
<img width="190" height="279" src="THE IMAGE URL 2" class="img-full wp-post-image" alt="" loading="lazy"> <h2>TITLE</h2>
</article>
</a></div>

Python code

self.driver.get(category_url)
WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div.archivePaging')))  # a div to see if page is loaded
movies_buttons = self.driver.find_elements_by_css_selector('img.img-full.wp-post-image')

print("Getting all the links!")
for movie in movies_buttons:
    self.driver.execute_script("arguments[0].scrollIntoView();", movie)
    movie.click()
    WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, 'infoFilmSingle')))
    print(self.driver.current_url)
    self.driver.back()
    WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div.archivePaging')))
            

Note that this code don't work now because i'm calling a movie object of an old page but that's not a problem because if i would just see the link i don't need to change page and so the session don't change.

An example based on what I understand you want to do - You wanna get the parent a tags href

Example

from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\Program Files\ChromeDriver\chromedriver.exe')

html_content = """
<div class="col-xl col-lg col-md-4 col-sm-6 col-6">
    <a href="https://www.link1.de">
        <article>
            <span class="year">2017</span>
            <span class="quality">4K</span>
            <span class="imdb">6.7</span>
            <img width="190" height="279" src="THE IMAGE URL" class="img-full wp-post-image" alt="" loading="lazy"> <h2>TITLE</h2>
        </article>
    </a>
</div>
<div class="col-xl col-lg col-md-4 col-sm-6 col-6">
    <a href="https://www.link2.de">
        <article>
            <span class="year">2019</span>
            <span class="quality">4K</span>
            <span class="imdb">8.0</span>
            <img width="190" height="279" src="THE IMAGE URL 2" class="img-full wp-post-image" alt="" loading="lazy"> <h2>TITLE</h2>
        </article>
    </a>
</div>
"""
driver.get("data:text/html;charset=utf-8,{html_content}".format(html_content=html_content))

Locate the image elements with its class and and walk up the element structur with .. in this case /../..

driver.get("data:text/html;charset=utf-8,{html_content}".format(html_content=html_content))
aTags = driver.find_elements_by_xpath("//img[contains(@class,'img-full wp-post-image')]/../..")
for ele in aTags:
  x=ele.get_attribute('href')
  print(x)
driver.close()

Output

https://www.link1.de/
https://www.link2.de/

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