Click the Follow Instagram button with Selenium C#

Asked

Viewed 256 times

1

I would like to know how to press the follow button of Instagram with Selenium, I have tried several ways and none I can press. Always says or that the element is not clickable or cannot find the element.

<span class="vBF20 _1OSdk"><button class="_5f5mN       jIbKX  _6VtSN  
yZn4P   ">Seguir</button></span>

This is the code of the button I want to click. I have tested it in the following ways.

driver.FindElement(By.CssSelector("button")).click()
driver.FindElement(By.ClassName("_5f5mN jIbKX _6VtSN yZn4P")).Click()
((IJavaScriptExecutor)driver).ExecuteScript("document.getElementsByClassName('_5f5mN jIbKX _6VtSN yZn4P')[0].click()");

I no longer know what to try ... someone knows how to do ? ...

NOTE: This last one works in the browser if I play on the console. But when I try for Selenium it does not find the Element to click.

NOTE: I tested the following codes below and also did not work

((IJavaScriptExecutor)driver).ExecuteScript("document.getElementsByTagName('button')[1].click()");
driver.FindElement(By.XPath("\\button:text()="Seguir"[1]")).Click();
((IJavaScriptExecutor)driver).ExecuteScript("document.evaluate(\"//button[text()='Seguir'][1]\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click()");
  • 1

    tries to swap xpath for //button[text()='Follow'][1] , this css ai looks like it is named dynamically

  • Question, is it for testing or for bot?

  • I’m testing and at the same time making a bot too. Lucas, I’ll test here and I’ll tell you if it worked.

  • Lucas Miranda unfortunately did not work the method he passed with xpath. https://prnt.sc/uu8i7n

  • a new attempt, try using the script executor to run it here Document.evaluate("/button[text()='Follow'][1]", Document, null, Xpathresult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click()

  • I’ll test and get back to you

  • Unfortunately it was an error - https://prnt.sc/uubv4s

  • ((Ijavascriptexecutor)driver). Executescript("Document.evaluate("//button[text()='Follow'][1]", Document, null, Xpathresult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click()"); that is the code .. It seems that Instagram won’t let the bot run .. pq by the console in the browser worked...

Show 3 more comments

1 answer

1

Changes class to an xpath that will definitely work. Example:

IWebDriver webdriver = WebDriverFactory.CreateWebDriver(Browser.Chrome, @"ChromeDriver", true);

webdriver.LoadPage(TimeSpan.FromSeconds(10), @"https://www.instagram.com/neymarjr/"); //entra no perfil 

IWebElement BtnSeguir = null;
BtnSeguir = webdriver.FindElement(By.XPath("//*[@id='react-root']/section/main/div/header/section/div[1]/div[2]/div/div/div/span/span[1]/button")); 
BtnSeguir.Click(); //clica pra seguir

Browser other questions tagged

You are not signed in. Login or sign up in order to post.