Selenium code to scroll Horizontally in a web-element which is loading lazily
Try one of these (tested on your first link):
WebElement scrollArea = driver.findElement(By.className("wrapper"));
// Scroll to div's most right:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollLeft = arguments[0].offsetWidth", scrollArea);
// Or scroll the div by pixel number:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollLeft += 250", scrollArea);
In order to scrollIntoView
you need the element
to exist in the DOM, so maybe that's why it didn't work for you. As for the window.scroll
you're trying to scroll the window and not inside an element.
How to do horizontal scroll using selenium webdriver
Simply use below for horizontal scroll
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.querySelector('table th:last-child').scrollIntoView();");
This should allow to scroll to the last column of the table
Selenium: horizontal scroll using Actions class
I have tested your scenarion over the site specified below, with horizontal and vertical scrolls.
Test URL « https://trello.com/b/LakLkQBW/jsfiddle-roadmap
;
Element Inner Vertical Scroll «
Selenium java:
WebElement element = driver.findElement(By.xpath("//div[@id='board']/div[1]/div[1]/div[2]") );
for (int i = 0; i < 5; i++) {
jse.executeScript("arguments[0].scrollTop += 200;", element);
}javascript
|
jquery:var objDiv = $x("//div[@id='board']/div[1]/div[1]/div[2]")[0];
console.log( objDiv );
objDiv.scrollTop += 100;
//objDiv.scrollTop = objDiv.scrollHeight;
Element Inner Horizontal Scroll «
Java Actions Class
WebElement horizontalbar = driver.findElement(By.id("board") );
Actions action = new Actions(driver);
Actions moveToElement = action.moveToElement( horizontalbar );
for (int i = 0; i < 5; i++) {
moveToElement.sendKeys(Keys.RIGHT).build().perform();
}
Scroll till the Element comes into view.
javascript
public void scroll_Till_Element(String id) {
WebElement element = driver.findElement(By.id( id ) );
jse.executeScript("arguments[0].scrollIntoView(true);", element);
}
Scroll till end of the page.
public void scrollPage() throws InterruptedException {
driver.get("https://stackoverflow.com/q/33094727/5081877");
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//body") );
Actions scrollDown = actions.moveToElement( element );
scrollDown.keyDown(Keys.CONTROL).sendKeys(Keys.END).build().perform();
//jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Thread.sleep( 1000 * 4 );
/*Actions scrollUP = actions.moveToElement( element );
scrollUP.keyDown(Keys.CONTROL).sendKeys(Keys.HOME).build().perform();*/
jse.executeScript("window.scrollTo(0, -document.body.scrollHeight)");
scroll_Till_Element( "answer-33142037" );
}
for javascript you can use any of these.
window.scrollBy(0,800);
window.scrollTo(0, -document.body.scrollHeight);
scroll(0, -document.body.scrollHeight);
@see
- Page scroll
JTable with horizontal scrollbar
First, add your JTable
inside a JScrollPane
and set the policy for the existence of scrollbars:
new JScrollPane(myTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Then, indicate that your JTable must not auto-resize the columns by setting the AUTO_RESIZE_OFF
mode:
myJTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
How to add a vertical and horizontal scroll bar in a JTextArea to view complete file - JAVA
You have to use
JScrollPane scrollBar = new JScrollPane(jTextArea1);
scrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollBar.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
And correct the position of the JButton. Try this code:
public class OpenFile {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OpenFile window = new OpenFile();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public OpenFile() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea jTextArea1 = new JTextArea();
jTextArea1.setText("Here is a text ... ");
jTextArea1.setBounds(10, 39, 402, 199);
frame.getContentPane().add(jTextArea1);
JScrollPane scrollBar = new JScrollPane(jTextArea1);
scrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollBar.setHorizontalScrollBarPolicy(
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scrollBar);
scrollBar.setBounds(13, 39, 413, 214);
JButton btnNewButton = new JButton("Read the text");
btnNewButton.setBounds(149, 10, 130, 21);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent arg0) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String nameFile = f.getAbsolutePath();
try {
FileReader reader = new FileReader(nameFile);
BufferedReader br = new BufferedReader(reader);
jTextArea1.read(br, null);
br.close();
jTextArea1.requestFocus();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
// TODO Auto-generated method stub
}
});
}
}
How to scroll horizontally a dynamic table in selenium web driver in java
In Python you could do
# driver init
# load page
scrollable_div_element = driver.find_element_by_id("hotTableParentDiv")
driver.execute_script("arguments[0].scrollLeft = arguments[0].scrollWidth", scrollable_div_element)
where arguments[0].scrollWidth
is the max scroll width for scrollable_div_element
. Therefore this example would horizontally scroll your div fully to the right edge.
Horizontal Scrolling Listener
Using the following code every time you scroll sideways a message gets printed out...
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
scene.setOnScroll(new EventHandler<ScrollEvent>(){
@Override
public void handle(ScrollEvent event) {
System.out.println("Scroll:" + event.getDeltaX());
}
});
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
One thing to consider:
Apparently when embedding a JFXPanel into a JFrame the sideway scrolling event is not getting passed.
Related Topics
How to Set a Description and an Example in Swagger With Swagger Annotations
Connection Java - Mysql:Public Key Retrieval Is Not Allowed
How to Get the Position of a Element in a Queue
Chrome Browser Is Not Opening in Selenium Webdriver
How Does Bufferedreader Read Files from S3
How to Find Index of First Vowel in a String
How to Handle Incorrect User Input into a Scanner
How to Upload a File and Json Data in Postman
Multipartexception: Current Request Is Not a Multipart Request
How to Beautifully Update a JPA Entity in Spring Data
How to Determine If a List of String Contains Null or Empty Elements
Pass Multiple Parameters to Rest API - Spring
How to Check Whether a Given String Is Valid Json in Java
How to Pass JavaScript Values to Scriptlet in Jsp
How to Join Results of Multiple Tables in Spring JPA Repository
How to Disable Error Highlighting in VS Code
Null Can Not Be Compared to Double - Why
How to Read File from Relative Path in Java Project? Java.Io.File Cannot Find the Path Specified