自动化Google Play音乐播放列表滚动与Python中的Selenium

时间:2017-11-25 18:21:23

标签: python selenium web-scraping automation

所以我试图自动滚动我的谷歌播放音乐播放列表,但它没有拿起,我不知道其他什么解决方案。正常滚动就像堆栈溢出一样,但我认为Google Music webapp使用了不同的东西。所以这是我目前的代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from bs4 import BeautifulSoup
from selenium.webdriver.common.action_chains import ActionChains

profile = webdriver.FirefoxProfile(r"C:\Users\Richard\AppData\Roaming\Mozilla\Firefox\Profiles\puom7wwz.default-1468625172145")
driver = webdriver.Firefox(executable_path=r'geckodriver.exe',firefox_profile=profile)
driver.get("https://play.google.com/music/listen?u=1#/pl/AMaBXymw0YGY7Y-z1B-FdrwkDCm7WdO_kyK4x4xQqn_vZsg56-VzwjsRKaQPypMs88oIKHn4qZd5aDbFbh5ojt6DpoZZCyHgCA%3D%3D")
driver.find_element_by_id("gb_70").click()

time.sleep(2)
element = driver.find_element_by_xpath("//*[contains(text(), 'William Thomas')]").click()
time.sleep(2)

password_field = driver.find_element_by_name("password")
password_field.clear()
password_field.send_keys("IcewindDale1995%")
password_field.send_keys(u'\ue007') #unicode for enter key
time.sleep(10)
driver.get("https://stackoverflow.com/questions/44370208/clicking-on-link-through-google-with-selenium-web-driver")

#driver.find_element_by_id("gba_70").click()
source = driver.page_source
soup = BeautifulSoup(source, "html.parser")
time.sleep(10)

#driver.execute_script("document.getElementById('descriptionWrapper').focus();")
#time.sleep(10)
driver.execute_script("window.scrollTo(0, 400)")
# i = 0
# while i<1000:
#         scroll = driver.find_element_by_id('descriptionWrapper').send_keys(Keys.PAGE_DOWN)
#         i+=1
driver.find_element_by_id("gba_70").click()

with open('page1.html', 'w', encoding="utf-8") as fid:
    fid.write(str(soup))
driver.close()

编辑:

该代码没有滚动,但它可以在Stack overflow网站上运行。 有解决方案的人吗?

1 个答案:

答案 0 :(得分:0)

问题在于Google Play音乐播放器本身无法滚动,您需要使浏览器专注于内部可滚动元素,类似于以下内容:

driver.find_element_by_css_selector('#music-content > div.detail-view.g-content.view-transition').click()

希望这个答案对您有帮助!