Bill Orton
Bill Orton

Reputation: 135

Selenium execute_script window.scrollTo not scrolling window

I am trying to click on the "Next" button at the bottom of a BusinessWire web page. I have some code that goes from the BusinessWire homepage to my desired search results page. I want to be able to click on that page's "Next" button, but I get the error message telling me that the "Next" element is not clickable at point(X,Y). The next button is at the bottom of the window. For some reason the

browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")

command is not scrolling as I would expect. Because the window is not scrolling as expected the element is not visible to be clicked (at least I believe that is the problem). I use this same command twice earlier in the code and it works fine in those two instances. Any help on getting the window to scroll would be greatly appreciated. Here is my code:

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
from selenium.webdriver.common.by import By

def BusinessWire():
    browser = webdriver.Chrome()
    browser.get('http://www.businesswire.com/portal/site/home/')
    search_box_element = browser.find_element_by_id('bw-search-
input')
    search_box_element.clear()
    search_box_element.send_keys('biotechnology')
    search_box_element.send_keys(Keys.ENTER)
    browser.execute_script("window.scrollTo(0, 
document.body.scrollHeight);")
    search_box_element_two = browser.find_element_by_id('more-news-
results')
    search_box_element_two.click()
    browser.execute_script("window.scrollTo(0, 
document.body.scrollHeight);")
    time.sleep(5)
    next_page_click_element = WebDriverWait(browser, 10).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="more-news-pagination"]/div/div[1]/div/a'))
    )
    next_page_click_element.click()

Upvotes: 2

Views: 5204

Answers (2)

smit9234
smit9234

Reputation: 361

Try using scrollIntoView of the element you want to scroll to, sorry this is in Java, but should work the same in python:

    driver.manage().window().maximize();
    driver.get("http://www.businesswire.com/portal/site/home/");
    wait = new WebDriverWait(driver, 10);
    driver.findElement(By.id("bw-search-input")).clear();
    driver.findElement(By.id("bw-search-input")).sendKeys("biotechnology");
    driver.findElement(By.id("bw-search-input")).sendKeys(Keys.ENTER);
    WebElement clicklink = driver.findElement(By.id("more-news-results"));
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", clicklink);
    clicklink.click();
    Thread.sleep(1000);
    WebElement clicknext = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"more-news-pagination\"]/div/div[1]/div/a")));
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", clicknext);
    clicknext.click();

Upvotes: 1

Bill Orton
Bill Orton

Reputation: 135

StackOverFlow won't let me post any more comments? The url is in the code. But the homepage url is not the problem page. The problem page is the search page after the home page. The search page can only be gotten to through a search on the homepage. My code does all this.

Upvotes: 0

Related Questions