Home > Blockchain >  How do I select the previous element by xpath?
How do I select the previous element by xpath?

Time:11-16

I need to select the first image I see (up the DOM) after the link. I have such a code, but for some reason it does not work, where can there be an error?

for doc in response.css('a::attr("href")'):
    if '/mans/' in doc.get():
        image = doc.xpath(f'//a[href={doc.get()}]/preceding::img[1]')
        print(image)

could there be a syntax error? here is an example of the analyzed page...

<tbody>
  <tr>
    <td>
                                                            <img src="Resources/Images/Product images, logos/Alta, Alta HR/Alta_Black_Calendar_Horizontal_Shadow.png" alt="Fitbit Alta with the time shown on the screen" />
                                                        </td>
                                                        <td>
                                                            <p><a href="https://help.fitbit.com/manuals/manual_alta_de.pdf" target="_blank">Deutsch</a>
                                                            </p>
                                                            <p><a href="https://help.fitbit.com/manuals/manual_alta_en_US.pdf" target="_blank">English</a>
                                                            </p>
                                                            <p><a href="https://help.fitbit.com/manuals/manual_alta_es.pdf" target="_blank">Español</a>
                                                            </p>
                                                        </td>
                                                        <td>
                                                            <p><a href="https://help.fitbit.com/manuals/manual_alta_fr.pdf" target="_blank">Français</a>
                                                            </p>
                                                            <p><a href="https://help.fitbit.com/manuals/manual_alta_it.pdf" target="_blank">Italiano</a>
                                                            </p>
                                                            <p><a href="https://help.fitbit.com/manuals/manual_alta_ja.pdf" target="_blank">日本語</a>
                                                            </p>
                                                        </td>
                                                        <td>
                                                            <p><a href="https://help.fitbit.com/manuals/manual_alta_ko.pdf" target="_blank">한국어</a>
                                                            </p>
                                                            <p><a href="https://help.fitbit.com/manuals/manual_alta_zh_CN.pdf" target="_blank">简体中文</a>
                                                            </p>
                                                            <p><a href="https://help.fitbit.com/manuals/manual_alta_zh_TW.pdf" target="_blank">繁體中文</a>
                                                            </p>
                                                        </td>
                                                    </tr>
                                                </tbody>

CodePudding user response:

Put an @ in front of the href in your xpath statement and it should work given that the element before the link is an image.

f'//a[@href={doc.get()}]/preceding::img[1]'
  • Related