我试图通过向下滚动在xpath中获取一个Webelement,但这很困难,因为该Webelement仅在向下滚动时出现,我的意思是此Webelement仅在向下滚动时出现,所以我不知道该怎么做,因为也许我必须先向下滚动一次,然后再向下滚动一次,直到出现我想要的网络元素为止,这取决于我要获得哪个元素。 我尝试以此向下滚动,但没有从下方获取网络元素。
JavascriptExecutor js = (JavascriptExecutor) getDriver();
js.executeScript("window.scrollBy(0," + bajar + ")");
waitFor(3).seconds();
我也尝试过此操作,但由于找不到WebElement而无法正常工作。
Point loc = getDriver().findElement(findBy(String.format
("//div[@class='windowViewMode-maximized active lafPageHost']//tbody//td//span/a[@title='%s']//ancestor::tr//child::td//span/a[@title='%s']",
nombre_cuenta, accion_relacional))).getLocation();
System.out.println(loc);
JavascriptExecutor js = (JavascriptExecutor) getDriver();
js.executeScript("javascript:window.scrollBy(0," + loc.y + ")");
System.out.println("El valor de y es " +loc.y);
System.out.println("Se hizo el scroll");
waitFor(1).seconds();
WebElement registroLista = findBy(String.format
("//div[@class='windowViewMode-maximized active lafPageHost']//tbody//td//span/a[@title='%s']//ancestor::tr//child::th//span/a[@title='%s']",
nombre_cuenta,accion_relacional));
baseMetodosPagina.clickJavaScript(registroLista);
顺便说一下,我正在使用硒和Java。
答案 0 :(得分:0)
未经测试,但类似的方法可能有效:
int LicksToTheCenterOfATootsiePop = 100;
int NumTries = 0;
int ec_Timeout = 10;
public void ScrollUntilElementFound(string xpath)
{
try
{
wait = new WebDriverWait([your webdriver], ec_Timeout);
List<WebElement> elements = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(xpath)));
if (elements.size() > 0) //may not need this, just return since timeout should be thrown if it's zero
{
return;
}
}
catch (Exception ex)
{
NumTries++;
if (NumTries<LicksToTheCenterOfATootsiePop)
{
((JavascriptExecutor)[your webdriver]).executeScript("window.scroll(0, document.height);");
ScrollUntilElementFound(xpath);
}
else
{
return;
}
}
}
ScrollUntilElementFound("[your XPATH to find Element]");
// now find element, and set NumTries back to zero if you plan on re-using
答案 1 :(得分:0)
您可以尝试在网页上查找特定的Web元素。
JavascriptExecutor js = (JavascriptExecutor) getDriver();
js.executeScript("arguments[0].scrollIntoView(true);", web element locator);
//use explicit wait for synchronization purposes.
通过这种方式,您的页面将滚动到该Web元素,然后对所需的元素执行操作。
答案 2 :(得分:0)
您需要滚动到该特定元素,并编写一个明确的等待条件,直到该元素可见为止,然后执行操作。这是示例代码:
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ScrollToElement{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait w= new WebDriverWait(driver, 10);
driver.get("https://semantic-ui.com/elements/button.html");
driver.manage().window().maximize();
// Scroll to specific Element
JavascriptExecutor js=(JavascriptExecutor) driver;
WebElement e =
driver.findElement(By.xpath("//button[contains(text(),'Negative')]"));
js.executeScript("arguments[0].scrollIntoView();",e);
w.until(ExpectedConditions.visibilityOf(e));
}
}