Advanced Techniques

Explore more about browser management by learning tab switching and URL navigation with Selenium.

Scroll focus to control

Due to JavaScript, sometimes certain controls are not visible on the web page, thus making Selenium unable to click on them. If we try to click on such controls, they will throw an error like : “Element is not clickable at point (1180, 43)”. The solution to this problem is scrolling the browser view. We can do this by:

driver.manage().window().setSize(1024, 208); // make browser small
elem = driver.findElement(By.name("submit_action_2"));
elem_pos = elem.getLocation().y   
driver.executeScript("window.scroll(0, " + elem_pos + ");"); // scroll
driver.sleep(300)    
elem.click();
driver.manage().window().setSize(1024, 768);

Switch between browser windows or tabs

As previously discussed in the section of (italic) hyperlinks, a “target='_blank' “ hyperlink opens up a web page in the new browser window or tab (depending upon the browser setting). So, this makes certain things problematic, as Selenium drives a browser within a scope of one browser window only. However, we can use Selenium’s switchTo() function to change the target browser window or tab.

Get hands-on with 1400+ tech skills courses.