Selenium Webdriver从上一页而不是当前页面查找元素

时间:2019-06-09 06:06:49

标签: python selenium selenium-webdriver

我正在尝试自动执行YouTube搜索,单击搜索结果,然后按照右侧的推荐视频进行操作。我写的代码转到youtube,进行搜索,然后单击视频,然后在浏览器中打开视频。但是,我无法带它单击推荐的视频之一。

问题似乎是,当我使用recommended_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 个答案:

答案 0 :(得分:0)

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。