Aman
Aman

Reputation: 129

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. 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)

Upvotes: 2

Views: 1242

Answers (1)

Aman
Aman

Reputation: 129

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.

Upvotes: 2

Related Questions