0

What should the program do? Click on every Button on the site.

enter image description here

By bybut = By.XPath("//span[@class='Button']");

var element = driver.FindElement(bybut);

IJavaScriptExecutor js = driver as IJavaScriptExecutor;

for (int i = 0; i < 99; i++)
{
    // Scroll element into view (orange rectangle on my picture)
    js.ExecuteScript("arguments[0].scrollIntoView(true);", element);

    // Time for scrolling
    System.Threading.Thread.Sleep(2000);

    // Click on Button
    driver.FindElement(By.XPath("//span[@class='Button']")).Click();
}

On the internet site, if you click on the button, it will be removed.

My Problem: If the For-Loop repeated, it comes to an error, because the next button is already in my view frame. How can I check, if a button is already into view?

Do you have any other solutions? Thanks in advance!

1 Answer 1

1

YOu can try below code :_

IJavaScriptExecutor js = driver as IJavaScriptExecutor;

    List<WebElement> displayedOptions = driver.FindElements(//span[@class='Button']);

    for (WebElement option : displayedOptions) 
     {
    try{

     js.ExecuteScript("arguments[0].scrollIntoView(true);", option );

     System.Threading.Thread.Sleep(2000);

        // Click on Button
        driver.FindElement(By.XPath("//span[@class='Button']")).Click();


    }catch(WebException E){


    }
Sign up to request clarification or add additional context in comments.

2 Comments

OpenQA.Selenium.ElementNotVisibleException: "element not visible"
I rewritten a little bit my code and now it works! Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.