如何查找仅在使用Selenium滚动后才可用的元素?

时间:2019-07-15 11:54:22

标签: python python-3.x selenium-webdriver scroll automation

我想在页面上找到仅在用户滚动到页面时才加载的元素。为了滚动到它,我必须找到它。但是,为了找到它,我必须滚动到它,以便它以html显示。有任何解决方法。

我尝试通过xpath查找元素,以确保没有可找到的此类元素。

channel_text = driver.find_element_by_xpath(f"//*[contains(text(), '{name_of_text_inside_tag}')]")

当我在开发人员模式下打开页面并通过xpath搜索时,找不到任何内容。但是,当我滚动到该元素时,它会通过相同的xpath找到它。

As you can see in this picture I searched for channel named worldbuilding. As the channel was visible it found it. Here however the channel was not visible and element was not found.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()

# get discord server
driver.get("https://discordapp.com/channels/393766374272663564")

# find channel on SCROLLABLE side menu
channel_text = driver.find_element_by_xpath(f"//*[contains(text(), 'channel-name')]")
print(channel_text) # prints empty list

不胜感激。

2 个答案:

答案 0 :(得分:2)

您可以创建一个循环,该循环将滚动到页面底部并搜索元素。

元素在滚动之后被加载,因此您需要等待。这会使您的脚本变慢,但更可靠。创建具有可容忍时间延迟的等待对象。由于您可能需要滚动几次,因此超时之前允许的时间应该很小。我建议您使用专门为此目的创建的等待对象,且其第二次超时不得超过5秒。

您还需要设置一个限制,以使其永远不会出现。

from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# create a dedicated wait object to wait for a brief period for the elements to be created.
wait = WebDriverWait(driver, 5)
# (...)
# find channel on SCROLLABLE side menu

# limit the number of scrolls
count = 0
while count < 5:
    driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
    try:
        channel_text = wait.until(
            EC.presence_of_element_located(
             (By.XPATH, f"//*[contains(text(), 'channel-name')]")
            )
        )
        break
    except TimeoutException:
        pass
    count += 1
else:
    # do whatever must be done if the element is never found.
    pass
print(channel_text)

答案 1 :(得分:-2)

它对我有用,试试这个

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("你的定位器")));