Selenium Python:如何在弹出窗口中向下滚动

Selenium Python:如何在弹出窗口中向下滚动

问题描述:

我正在开发一个Linkedin web scraping项目。我想获取某人感兴趣的公司列表(注意我没有使用API​​)。这是一个动态的网站,所以我需要向下滚动,同时刮取公司的名称。我知道如何在MAIN窗口中执行此操作,但由于Interest是一个弹出窗口这个滚动解决方案不起作用。到目前为止,我的代码是:

I am working on a Linkedin web scraping project. I am trying to get the list of companies that interest someone (notice I am not using the API). It is a dynamic website, so I would need to scroll down while scraping the names of the companies. I know how to do this in the MAIN window, but since Interest are a pop-up window this solution to scroll does not work. My code so far was:

from selenium.webdriver.common.keys import Keys
bar = driver.find_element_by_xpath('//ul[@class="entity-list row"]')
bar.send_keys(Keys.END)

由于它不起作用,我也尝试过:

Since it didn't work, I also tried:

bar = driver.find_element_by_xpath('//ul[@class="entity-list row"]')
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', bar)

问题在于我不是在弹出窗口中操作,而是在主窗口中操作,因此它没有达到预期的效果。

The problem is that I am not acting on the pop up window but in the main one so it has not the desired effect.

在此处输入图片说明

您可以尝试在弹出窗口中找到元素(可以聚焦的元素),例如一些锚点:

You can try to find element inside popup (the one that can be focused), for example some anchor:

element_inside_popup = driver.find_element_by_xpath('//div[@class="entity-list-wrapper ember-view"]//a')

然后使用下面的代码滚动弹出窗口:

and then use below code to scroll popup down:

from selenium.webdriver.common.keys import Keys

element_inside_popup.send_keys(Keys.END)