首页 > 解决方案 > Xpath 问题,试图根据另一个元素的结果定位一个元素

问题描述

我正在尝试使用此代码:

from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver

from textblob import TextBlob


driver = webdriver.Chrome(executable_path=r'/home/littlejiver/Downloads/chromedriver_linux64/chromedriver')
wait = WebDriverWait(driver, 10)
driver.get("https://zooqle.com/movie/toy-story-l9.html?tg=7")

torrent_titles = driver.find_elements_by_xpath('//*[@id="body_container"]/div/div[1]/div[2]/div/table/tbody/tr/td[2]/a')

torrent_found_on_zooqle = False

for title in torrent_titles:
    title_to_check = TextBlob(title.text)
    if title_to_check.detect_language() == "en":
        if int(title.find_element_by_xpath('//*[@id="body_container"]/div/div/div/div/table/tbody/tr/td[5]/div/div[1]').text) == 10:
            title.click()
            torrent_found_on_zooqle = True
            break
        else:
            print("not enough seeds")

检查红色突出显示的元素是否通过然后给我黄色突出显示的信息如果它符合条件

在此处输入图像描述

现在,我的代码正在做的是检查标题是否为英文,然后检查种子,但检查表中的第一行而不是对应的行。

您可以提供的任何帮助将不胜感激。

谢谢!

小吉弗

标签: pythonseleniumxpath

解决方案


您必须从 a 标记 xpath,因此您没有正确转到下一个元素。

torrent_titles = driver.find_elements_by_xpath('//table/tbody/tr/td[2]/a')

torrent_found_on_zooqle = False

for title in torrent_titles:
    title_to_check = TextBlob(title.text)
    if title_to_check.detect_language() == "en":
        if title.find_element_by_xpath("following::td[@class='text-nowrap']/div[1]/div[1]").text == '10':
            title.click()
            torrent_found_on_zooqle = True
            break
        else:
            print("not enough seeds")

推荐阅读