简体   繁体   English

无法滚动到 selenium python 中的元素

[英]Can't scroll to an element in selenium python

I'm trying to visit next page after searching.我试图在搜索后访问下一页。 I'm getting the first page, but in order to go to next page i need to scroll down to click next page element.I've tried different methods as shown in the code to scroll down the webpage but despite all attempt i'm still getting ElementNotVisibleException error.我正在获取第一页,但为了进入下一页,我需要向下滚动以单击下一页元素。我尝试了代码中所示的不同方法来向下滚动网页,但尽管我尝试了所有仍然收到 ElementNotVisibleException 错误。 Can anyone tell me why the scrolling isn't working.谁能告诉我为什么滚动不起作用。

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import time
driver = selenium.webdriver.PhantomJS(executable_path=r'C:\phantomjs-2.1.1-windows\bin\phantomjs.exe')
driver.get('https://www.texasbar.com/am/Template.cfm;jsessionid=7EB4486736A022DC2AB99C24E9071D70.cfusion?Section=Find_A_Lawyer&template=/Customsource/MemberDirectory/Search_form_client_main.cfm&CFID=39868973&CFTOKEN=2f314a81f05a55c6-469AE4D3-91FD-AA7B-9D59C8F7DB39779F')
time.sleep(4)
elem = driver.find_element_by_id("Zip").send_keys("75001"+"\n")
time.sleep(6)
new = driver.find_element_by_css_selector("form[name=\"HiddenFormFields\"] > a.next-btn.btn")
driver.execute_script("window.scrollTo(0, 7664)")
#driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
#driver.send_keys(Keys.END)
new.click()
time.sleep(4)
pagesource = driver.page_source
soup = BeautifulSoup(pagesource, 'html.parser')
print(soup)

Finally i have solved the problem.最后我解决了这个问题。 Before getting the url, i have set the browser window size driver.set_window_size(1124,850) and it's solved.在获取 url 之前,我已经设置了浏览器窗口大小driver.set_window_size(1124,850)并已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM