Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Selenium-为什么搜索结果返回Web serach中相同元素的列表_Python 3.x_Selenium_Selenium Webdriver_Firefox - Fatal编程技术网

Python 3.x Selenium-为什么搜索结果返回Web serach中相同元素的列表

Python 3.x Selenium-为什么搜索结果返回Web serach中相同元素的列表,python-3.x,selenium,selenium-webdriver,firefox,Python 3.x,Selenium,Selenium Webdriver,Firefox,我正在使用firefox浏览器在python selenium上尝试一个简单的程序。我想搜索一些东西并获取列表中的所有结果。例如,在youtube上搜索文本并打印所有结果的外部参照和标签 下面是我的代码。结果会打印第1个元素X的次数 class youtube: base_url = "https://www.youtube.com/" search_textbox_xpath = "//input[@id='search']" search_button_id = "search-icon-

我正在使用firefox浏览器在python selenium上尝试一个简单的程序。我想搜索一些东西并获取列表中的所有结果。例如,在youtube上搜索文本并打印所有结果的外部参照和标签

下面是我的代码。结果会打印第1个元素X的次数

class youtube:
base_url = "https://www.youtube.com/"

search_textbox_xpath = "//input[@id='search']"
search_button_id = "search-icon-legacy"

all_results_xpath = "//ytd-video-renderer[contains(@class,'ytd-item-section-renderer')]"
individual_result_href_xpath = "//a[@id='video-title']"
individual_result_text_xpath = "//a[@id='video-title']/yt-formatted-string[contains(@class,'ytd-video-renderer')]"

def search_youtube(self, search_text):
    try:
        b = Browser()
        handle = b.open_browser(self.base_url)
        time.sleep(8)

        search_textbox = handle.find_element_by_xpath(self.search_textbox_xpath)
        #search_textbox.clear()
        search_textbox.send_keys(search_text)

        handle.find_element_by_id(self.search_button_id).click()
        time.sleep(8)

        all_results = handle.find_elements_by_xpath(self.all_results_xpath)

        for each_result in all_results:
            print (each_result)
            href = each_result.find_element_by_xpath(self.individual_result_href_xpath).get_attribute('href')
            print(href)
            text = each_result.find_element_by_xpath(self.individual_result_text_xpath).text
            print(text)

    finally:
        b.close_browser()


if __name__ == '__main__':
    y = youtube()
    y.search_youtube('upcoming animated movies 2020')
输出:

2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片) 2020年最佳即将上映的动画和家庭电影(预告片)

我可以看到在“每个结果”中引用了不同的元素id。那么为什么它会打印相同的第一个结果X次呢?我打开了相同的搜索,我可以在源代码中看到所有搜索结果


提前谢谢

检查:(基本上您的XPATH仍在搜索整个上下文…使用“//”,这将其限制为当前WebElement的子元素。)非常感谢,pcalkins!它与这个简单的改变一起工作!