Press the Instagram Follow Buttons by utilizing Selenium with Java

click here for image description

Hello, I am currently working on automating the process of clicking follow buttons using Java. However, I'm encountering difficulties when trying to use JavascriptExecutor within a for loop. Below is the code snippet I'm working with:

     List<WebElement> clickOnFollowButton = driver.findElements(By.xpath("//button[contains(text(),'Follow')]"));
        for (int i = 0; i < clickOnFollowButton.size(); i++) {
            driver.findElements(By.xpath("//button[contains(text(),'Follow')]")).get(i).click();

            ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", clickOnFollowButton);

        }

I would greatly appreciate any assistance you can provide. Thank you.

Answer №1

In my opinion, the code "arguments[0].scrollIntoView();" should be placed before the .click method.

Additionally, it is crucial to assign each member in the list as a WebElement variable before using it with the JavascriptExecutor and clicking on it:

for (int i = 0; i < clickOnFollowButton.size() ; i++) {
    WebElement element = driver.findElements(By.xpath("//button[contains(text(),'Follow')]")).get(i);
    ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", element);
    element.click();
}

Alternatively, you can achieve the same result by looping through the list using a for-each loop like this:

List<WebElement> clickOnFollowButton = driver.findElements(By.xpath("//button[contains(text(),'Follow')]"));
for(WebElement element: clickOnFollowButton) {
    ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", element);
    element.click();
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for centering a background image:

I have been attempting to set a background image at the center, but all my efforts have failed. Here is the code I have tried: <a href="#" style="background-position:center center;"> <img src="https://firebasestorage.googleapis.com/v0/b/g ...

How can you make your Selenium script wait until your AutoIt script finishes running?

I need Selenium to wait until the Autoit Script is fully completed. Currently, when I execute the TestNG.xml file, it runs all the @Test methods in order of priority and within 5 seconds, TestNG console displays that all tests have passed. However, my Aut ...

Enhance Android Experience: Display AppData in DocumentsUI File Browser Menu

Accessing app data folders on newer Android versions 11 and above can be challenging. However, it is still possible to access them using an internal file browser such as com.android.documentsUi By opening two windows in split view, we can modify or move ...

Choose an option from the drop-down menu in Selenium to retrieve its value

I am trying to choose a value from a dropdown menu using Selenium. The specific value I want is "Other." See the screenshot below: https://i.stack.imgur.com/k4y1K.png The xpath for the dropdown element is: //nz-select[@formcontrolname='selectedIntegr ...

ExtJS web displays appear differently when viewed on Chrome's toggle device mode versus a mobile browser

Greetings, I have been working on developing a web application that can be accessed through a mobile browser. Initially, I created the web application and tested it in mobile mode using Chrome's simulation feature. https://i.sstatic.net/aAvVW.jpg H ...

Guide: Interacting with the Sign up button on Spotify's registration page with Python Selenium

Having trouble clicking the "sign up" button on the Spotify registration page. I have located the xpath and css for the button but still can't click it. I even attempted to use "click text" but encountered the same issue. WebDriverWait(driver,10).unti ...

Is there a way to ensure that the table headers are printed on every page when using Google Chrome

When printing documents in Chrome browser, the table header (thead) does not appear on every page. I want to ensure that the table header is displayed on each printed page, just like it is done with IE and Firefox. However, Chrome does not support this fea ...

Exploring the Customer and Product List Connection in Java Supermarket Software with UML Modeling

I'm in the process of developing a supermarket software that involves both customers and owners logging in to use my Java swing-based GUI system. Once logged in, customers can access product information, while owners have the ability to view products ...

Align the Media center div within a column utilizing Bootstrap

I have a template in html with a media object containing a logo and text, both within a column of width 5. Currently, it is not centered and aligned to the left. I am looking for a way to centrally align the media block within the column, regardless of its ...

Accordion is having trouble expanding fully

My accordion is causing some trouble. When I try to open one section, both sections end up opening. I want it to toggle, so that only one section opens at a time based on my click. Switching the display from flex to column fixes the issue, but I don' ...

The transparency of the TABLE must only be applied to a specific section

I need the table to have a transparent bottom and a clear top for better readability. Here is my attempted solution: .preview { background: -moz-linear-gradient(top, rgba(255,255,255,0) 25%, rgba(255,255,255) 100%); background: -webkit-gradie ...

Setting up button value dynamically according to dropdown selection in Angular 2

I'm a beginner in angular 2 and I have a button with a dropdown. When I select an option from the dropdown, I want its value to be displayed on the button. The CSS code for styling the dropdown is provided below: .common_select_box { width: 100%; } ...

What could be causing my navigation bar to malfunction?

Being fairly new to HTML and CSS, I have been struggling with my nav bar. Despite multiple attempts to fix it, the dropdown items do not appear when hovered over. Changing display: none; to display:block; only results in the items dropping down to the next ...

Using local file paths in v-lazy-image does not function properly

When trying to use the v-lazy-image plugin for my page banner with local image files, it does not seem to work. <v-lazy-image srcset="../../../assets/images/banners/Small-Banner.svg 320w, ../../../assets/images/banners/Big-Banner.svg 480w&quo ...

What is the best way to retrieve the text of a capybara element while also capturing any leading whitespace?

My challenge lies in creating an rspec/capybara/selenium spec that verifies whitespace. The scenario involves filling out a form and displaying the information as a row in a table after saving it. However, I am encountering an issue where calling `.text` ...

Can the style of certain words within a paragraph be altered using CSS only, without the need for any HTML tags?

Is it feasible to alter the appearance of certain words within a paragraph using CSS solely, without applying any HTML tags to those particular words? Awaiting a positive response. Thank you! ...

React: The row flex-direction is not being applied as expected

Although I know there are countless identical questions out there, mine lacks context and is as generic as they come. Why isn't the flex-direction row property working? React View return ( <Fragment> <h1>The About us page.</ ...

Creating a navigation bar that smoothly slides into view from the top

In my Angular app (version 2+), the current code I have is: .header { background: rgba(white, 0); &.fixed-top { background: rgba(white, 1); border-bottom: solid whitesmoke 1px; position: fixed; top: 0; right: 0; left: 0; ...

Python for speedy extraction of dynamic content

Is there a way to scrape dynamic content generated in JavaScript using Python without relying on a browser like Selenium? I am familiar with BeautifulSoup, which is fast but does not evaluate JavaScript, and Selenium, which can interpret JS but comes with ...

When scrolling, apply a CSS class to a div element once it becomes visible on the

I'm in the process of developing a timeline feature for my website, and I am facing an issue where the addClass function is being applied to all sections, even those that are not currently visible on the screen during scrolling. If you would like to ...