当前位置: 首页 > 知识库问答 >
问题:

Selenium webdriver从上一页查找元素,而不是从当前页查找元素

云文栋
2023-03-14

我试图自动化YouTube搜索,点击搜索结果,然后跟随右侧的推荐视频。我写的代码进入youtube,进行搜索,点击视频,视频就会在浏览器上打开。但是,我不能带它点击其中一个推荐的视频。

问题似乎在于,当我使用refromended_videos=driver.find_elements_by_id(“video-title”)从右边获取推荐视频元素的列表时,我得到的是上一页的列表(当我第一次输入单词并获得搜索结果时)。

当我使用driver.get(url)直接转到视频链接时,代码确实可以正常工作,而不是先进行搜索。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
import random

seed = 1
random.seed(seed)

driver = webdriver.Firefox()
driver.get("http://www.youtube.com/")

element = driver.find_element_by_tag_name("input")

# Put the word "history" in the search box and hit enter
element.send_keys("history")
element.send_keys(Keys.RETURN)

time.sleep(5)

# Get a list of elements (videos) that get returned by the search
search_results = driver.find_elements_by_id("video-title")

# Click randomly on one of the first five results
search_results[random.randint(0,4)].click()

# Go to the end of the page (I don't know if this is necessary
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.END)

time.sleep(10)

# Get the recommended videos the same way as above. This is where the problem starts, because recommended_videos essentially becomes the same thing as the previous page's search_results, even though the browser is in a new page now.

recommended_videos = driver.find_elements_by_id("video-title") 

recommended_videos[random.randint(0,4)].click()

所以当我点击(最后一行)时,我得到了错误

ElementNotInteractableException: Element <a id="video-title" class="yt-simple-endpoint style-scope ytd-video-renderer" href="/watch?v=1oean5l__Cc"> could not be scrolled into view

共有1个答案

阎冠玉
2023-03-14
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
import random

seed = 1
random.seed(seed)

driver = webdriver.Chrome()
driver.get("https://www.youtube.com")

element = driver.find_element_by_tag_name("input")

# Put the word "history" in the search box and hit enter
element.send_keys("history")
element.send_keys(Keys.RETURN)

time.sleep(5)

# Get a list of elements (videos) that get returned by the search
search_results = driver.find_elements_by_id("video-title")

# Click randomly on one of the first five results
search_results[random.randint(0,10)].click()

# Go to the end of the page (I don't know if this is necessary

#
time.sleep(4)

# Get the recommended videos the same way as above. This is where the problem starts, because recommended_videos essentially becomes the same thing as the previous page's search_results, even though the browser is in a new page now.
while True:
    recommended_videos = driver.find_elements_by_xpath("//*[@id='dismissable']/div/a")
    print(recommended_videos)
    recommended_videos[random.randint(1,4)].click()
    time.sleep(4)

我也是全新的,谢谢你给我硒的兴趣。愿此代码对您有所帮助。如果需要,请将驱动程序改为firefox。

 类似资料:
  • 问题内容: 我正在尝试找到一种比使用以下方法更好的方法: 我有一个页面,其中的数据可以更改,从而使页面高度的大小可以更改。因此,当我今天单击位于上的元素时,明天可能会找到它,并且找不到该元素,并且测试用例失败。 那么,你们用什么来定位大小变化的页面上的元素? 问题答案: 我开始使用下面的方法,似乎效果很好。谢谢大家的帮助。

  • 我尝试了这些方法,甚至放置等待,但仍然没有找到元素:

  • 在一个页面中有很多不同的策略可以定位一个元素。在你的项目中, 你可以选择最合适的方法去查找元素。Selenium提供了下列的方法给你: find_element_by_id find_element_by_name find_element_by_xpath find_element_by_link_text find_element_by_partial_link_text find_eleme

  • 我的WebDriver脚本只是在第1页(产品展示页)上找到元素,然后单击第1个元素,看看它是否工作,然后导航回产品展示页。 它抛出过时的元素引用错误,并且没有单击页面上的第二个元素,表示元素没有附加到页面。 代码是:

  • 我对在Selenium中使用python还很陌生。

  • 主要内容:检查百度首页,编辑网页代码,检查网页结构对于一个优秀的爬虫工程师而言,要善于发现网页元素的规律,并且能从中提炼出有效的信息。因此,在动手编写爬虫程序前,必须要对网页元素进行审查。本节将讲解如何使用“ 浏览器”审查网页元素。 浏览器都自带检查元素的功能,不同的浏览器对该功能的叫法不同, 谷歌(Chrome)浏览器称为“检查”,而 Firefox 则称“查看元素”,尽管如此,但它们的功却是相同的,本教程推荐使用谷歌浏览器。 检查百度首页 下