简体   繁体   English

滚动到一个WebElement并单击它

[英]Scrolling to a WebElement and clicking on it

I am new to automation and am practicing on the flipkart website. 我是自动化的新手,并且正在flipkart网站上练习。 On the page: 在页面上:

http://www.flipkart.com/mobiles/pr?sid=tyy,4io&otracker=clp_mobiles_CategoryLinksModule_0-2_catergorylinks_11_ViewAll http://www.flipkart.com/mobiles/pr?sid=tyy,4io&otracker=clp_mobiles_CategoryLinksModule_0-2_catergorylinks_11_ViewAll

... when I try to click an element that is not in view of the page by scrolling to it, I get the exception: Element is not clickable ...当我尝试通过滚动查看不在页面视图中的元素时,出现异常: Element is not clickable

Below is the code: 下面是代码:

WebElement mobile = driver.findElement(By.xpath ("//a[@title='Apple iPhone 6S (Silver, 128 GB) ']"));
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].scrollIntoView();", mobile);
mobile.click();

I believe this issue is occurring because of the header available in flipkart: even though the window is getting scrolled to that particular element, the header is covering the element so it's not possible to click on it. 相信由于flipkart中可用的标题而发生了此问题:即使窗口正在滚动到该特定元素,标题也覆盖了该元素,因此无法单击它。

Can anyone help resolve this? 谁能解决这个问题?

you can try like this 你可以这样尝试

  1. Case where you want to click on a element that is not in view of the page (without scrolling) try below 如果您想单击不在页面范围内的元素(无滚动),请尝试以下方法

     public static void main(String[] args) throws InterruptedException { WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get( "http://www.flipkart.com/mobiles/pr?sid=tyy,4io&otracker=clp_mobiles_CategoryLinksModule_0-2_catergorylinks_11_ViewAll"); driver.manage().window().maximize(); // Take everything on the page in list first . List<WebElement> completecalContent = driver.findElements(By.xpath("//*[@class='fk-display-block']")); System.out.println(completecalContent.size()); // printing all elements for (int i = 0; i < completecalContent.size(); i++) { System.out.println("Print complete Content : " + completecalContent.get(i).getText()); if (completecalContent.get(i).getText().equals("Apple iPhone 5S (Space Grey, 16 GB)")) { // move to a specific element ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", completecalContent.get(completecalContent.size() - 1)); // move slightly up as blue header comes in the picture ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,-100)"); // then click on the element completecalContent.get(i).click(); } } } 
  2. Case where you want to scroll then in that case update above code with these lines. 您要滚动的情况,在这种情况下,请使用这些行更新上面的代码。

A. if you want to scroll to the bottom of the page then 答:如果要滚动到页面底部,则

((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");

B. if u want to scroll to a specific element then try this B.如果您想滚动到特定元素,请尝试此操作

WebElement element = driver.findElement(By.xpath("xpath to element"));
        ((JavascriptExecutor) driver).executeScript(
                "arguments[0].scrollIntoView();", element);

C. if you want to scroll on the basis of coordinates then try this C.如果要基于坐标滚动,请尝试此操作

((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");

Instead of scrolling up to web element you can try scrolling to little bit down in page like 除了向上滚动到Web元素外,您还可以尝试向下滚动到类似页面的页面

 JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(250, 0)"); //x value '250' can be altered 

Else you can try scrolling to element which is good enough above to required element. 否则,您可以尝试滚动到足以胜过所需元素的元素。 It means in code you tried instead of taking required webelement just scroll upto web element above the required so that the header does not cover required element. 这意味着在您尝试的代码中,没有采用必需的web元素,而是仅滚动到所需元素上方的web元素,以便标头不覆盖必需元素。

Thank You, Murali 谢谢,穆拉利

Hey if you are not certain about the element's position on the page, you can find the co-ordinates at run time and then execute your text. 嘿,如果不确定元素在页面上的位置,可以在运行时找到co-ordinates ,然后执行文本。

You can get elements co-ordinate by using Point 您可以使用Point获得坐标

Point point = element.getLocation();
int xcord = point.getX();  
int ycord = point.getY();

You can also get the dimensions of a webelement like its Height and Width using Dimension 您还可以使用Dimension获取网络元素的尺寸,例如其HeightWidth

Once you have the x and y co-ordinates and you have its dimensions. 一旦有了xy坐标,就可以确定其尺寸。 You can write your code to scroll till that particular co-ordinates on the page. 您可以编写代码来滚动浏览页面上的特定坐标。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM