我正在尝试自动化WhatsApp Web应用程序。 contacts div有一个自定义滚动条。我必须发布消息的组不会显示在网页上,因此无法单击。如何滚动到该特定组?我尝试过以下代码,但它无效。
@Test
void begin(){
openBrowser();
driver.get("https://web.whatsapp.com/");
sleep(10000);
WebElement scroll = driver.findElement(By.id("pane-side")); // locator of contacts div.
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", scroll);
//Thread.sleep(500);
//WebElement groupName = driver.findElement(By.xpath("//span[text()='Word of the day']")); //locator of group
}
答案 0 :(得分:0)
自定义滚动条的辅助类:
public class ScrollHelper {
private static String VeticalScrollScript = "arguments[0].scrollTop = arguments[1]";
private static String HorizonScrollScript = "arguments[0].scrollLeft = arguments[1]";
private JavascriptExecutor jsExecutor;
public ScrollHelper() {
this.jsExecutor = (JavascriptExecutor) driver;
}
public ScrollHelper asHorizon(scrollTarget, scrollContext) {
if(isPresentHorizonScrollBar(scrollContext)) {
int offset = scrollTarget.getLocation().getX() -
scrollContext.getLocation().getX();
this.jsExecutor.executeScript(HorizonScrollScript, scrollContext, offset);
}
return this;
}
public ScrollHelper asVertical(scrollTarget, scrollContext) {
if(this.isPresentVerticalScrollBar(scrollContext)) {
int offset = scrollTarget.getLocation().getY() -
scrollContext.getLocation().getY();
this.jsExecutor.executeScript(VeticalScrollScript, scrollContext, offset);
}
return this;
}
private boolean isPresentHorizonScrollBar(WebElement scrollContext) {
String script = "return arguments[0].scrollWidth > arguments[0].clientWidth;";
return (Boolean) (this.jsExecutor.executeScript(script, scrollContext));
}
private boolean isPresentVerticalScrollBar(WebElement scrollContext) {
String script = "return arguments[0].scrollHeight > arguments[0].clientHeight;";
return (Boolean) (this.jsExecutor.executeScript(script, scrollContext));
}
}
对于你的情况:
ScrollHelper scroll = new ScrollHelper(driver);
scroll.asVertical(
scrollTarget, // the group
scrollContext // the element who owns the custom scroll bar
);
在下面的情况下,scrollContext
是绿线突出显示的pre
,
不是它的父<div class="post-text">
或其他元素。您需要找出拥有自定义滚动条的正确元素作为scrollContext
的值。
找到正确的scrollContext
的方法是看到元素具有CSS样式over-flow
,如果取消选中over-flow
之前的复选框,您会注意到滚动条将消失,然后来检查时回来。
答案 1 :(得分:0)
尝试这样的事情? (调整你的项目!)
while (true) {
try {
WebElement backgroundDivInsideScrollingPane = driver.findElement(By.xpath("//div[@id='idOfAnElementInTheScrollingPane']"));
backgroundDivInsideScrollingPane.sendKeys(Keys.PAGE_DOWN);
WebElement elementToBeFound = driver.findElement(By.xpath("//myElementLocatorHere"));
break;
} catch (Exception ignored) {
// Exception because element can't be found yet - Ignore!
}
}
答案 2 :(得分:0)
使用此代码的Whatsapp Messaging部分 您将可以选择Page Down和Page Up。 注意: 100%工作:)
消息详细信息向下滚动和向上滚动
private void SyhMhzScrollDetailPageUP()
{
IWebElement scroll = drv.FindElementByXPath("//div[@class='_1ays2']");
scroll.SendKeys(Keys.PageUp);
}
private void SyhMhzScrollDeatilPageDown()
{
IWebElement scroll = drv.FindElementByXPath("//div[@class='_1ays2']");
scroll.SendKeys(Keys.PageDown);
}
邮件列表向下滚动和向上滚动
private void SyhMhfzMesgListScrollPageUP()
{
IWebElement scroll = drv.FindElementByXPath("//div[@data-tab='4']");
scroll.SendKeys(Keys.Up);
}
private void SyhmhfzMesgListPageDown()
{
IWebElement scroll = drv.FindElementByXPath("//div[@data-tab='4']");
scroll.SendKeys(Keys.Down);
}
您也可以在此处使用Nuget软件包。 https://www.nuget.org/packages/Bekra.Whatshapp_Scroll_Down_Up/1.0.0