我需要使用selenium webdriver
在网页上使用滚动条向下移动我使用了以下代码
Actions dragger = new Actions(driver);
WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("/html/body/section[2]/div/div[2]/div/div/div"));
int numberOfPixelsToDragTheScrollbarDown = 5000;
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
仍然没有向下移动... xpath根据滚动条的位置而改变...
答案 0 :(得分:1)
如果您尝试通过向下滚动来查找某个元素,则以下代码将滚动,直到该元素处于视图中。
WebElement element = driver.findElement(By.id("id_of_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500);
//do anything you want with the element
答案 1 :(得分:1)
我的代码是在python中...希望它可以帮助你,你可以重现相同的java
actionChains = ActionChains(driver)
option=driver.find_element_by_class_name("mCSB_dragger_bar")
actionChains.click_and_hold(option).perform()
actionChains.move_by_offset(0,5000).perform()
actionChains.release()
上述代码可以简化为
actionChains = ActionChains(driver)
option=driver.find_element_by_class_name("mCSB_dragger_bar")
actionChains.click_and_hold(option).move_by_offset(0,5000).release().perform()
答案 2 :(得分:0)
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0,100)");
答案 3 :(得分:0)
通过
scroll=By.xpath("//*[@id='aspnetForm']/center/div/div[2]/table/tbody/tr[2]/td[1]/table/tbody");
WebElement scrollUp = driver.findElement(scroll);
scrollUp.sendKeys(Keys.PAGE_DOWN);
scrollUp.sendKeys(Keys.PAGE_DOWN);
scrollUp.sendKeys(Keys.PAGE_DOWN);
scrollUp.sendKeys(Keys.PAGE_DOWN);
向上滚动:
scrollUp.sendKeys(Keys.PAGE_DOWN);
答案 4 :(得分:0)
对于Java,代码如下:
public void moveOverElement(WebElement element)
{
Actions actions = new Actions(driver);
actions.clickAndHold(element).moveByOffset(0,5000).release().perform();
}
对于WebElement的定义,对于webelement,您只需要定义路径:
@FindBy (xpath = ".//*[contains(@class, 'link-name') and text() = 'QAEbox']")
private WebElement createdQABoxElement;
答案 5 :(得分:0)
动作拖动器=新的动作(驱动程序); WebElement draggablePartOfScrollbar = driver.findElement(By.xpath(“ / html / body / section [2] / div / div [2] / div / div / div”));; int numberOfPixelsToDragTheScrollbarDown = 5000; dragger.moveToElement(draggablePartOfScrollbar).clickAndHold( draggablePartOfScrollbar )。moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release()。perform();
此代码经过以下更正对我有效。
它起到了魅力。感谢您的代码。