1

I am trying to click on the "connect" button of linkedin and I can not, I have already tried in every possible way.

Explicit wait:

WebDriverWait espere_estar_pronto_para_clicar = new WebDriverWait(driver, 50);          espere_estar_pronto_para_clicar.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Conectar")));

HTML Page Structure:

<button aria-label="Conecte-se a Eduardo G. K. Perez." class="search-result__actions--primary button-secondary-medium m5" data-ember-action="" data-ember-action-1876="1876">Conectar</button>

Click Attempts:

Attempt 1:

List<WebElement> conectar = driver.findElements(By.xpath("//button[text()='Conectar']"));
conectar.click();

Attempt 2:

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(98,75, 32)");

Attempt 3:

driver.findElement(By.xpath("//*[@class='search-result__actions--primary.button-secondary-medium.m5']/button/text()")).click();

error:

Expected condition failed: waiting for element to be clickable: By.partialLinkText: Conectar (tried for 50 second(s) with 500 MILLISECONDS interval)

Paulo Roberto
  • 1,498
  • 5
  • 19
  • 42
  • What happens if you do not execute the WebDriverWait? You should try commenting that line out, then using the debugger to step through the code giving plenty of time for the button to be clickable. If you are able to click the button using the debugger, then it must be something wrong with the wait statement. Maybe the expected condition of the button will never be reached, so that statement will always fail. – st0ve Jul 20 '17 at 17:36

3 Answers3

0

Have you tried to take the XPath direct from a browser? (Chrome example below)

  1. Right click on the element
  2. Inspect Element
  3. Right click on DOM
  4. Select Copy
  5. Click XPath
  6. Paste in By.xpath(TEXT_COPIED)

If isn't that, I guess is the way you are trying to wait the button load.

Yoqueuris
  • 1
  • 3
  • Yes, I did this and it generates this xpath // * [@ id = "ember3060"] / button, however for each "connect" button on the page it generates a different ID and I will loop it by clicking several "connect" , I can not get stuck to the xpath of only 1 button, although the text "connect" is on all buttons, xptah varies greatly, since the embed is dynamic, embed3060, embed 3061, embed2198 and etc. – Paulo Roberto Jul 20 '17 at 17:41
  • If the IDs are dynamically you should have a good logic to find exactly elements. The point is how you are doing the wait logic. Have you seen this QA? https://stackoverflow.com/questions/11736027/webdriver-wait-for-element-using-java – Yoqueuris Jul 20 '17 at 17:45
0

The below code will do tested on my local, assuming you are going on particular user profile page whom you want to connect once you signed-in and then clicking on Connect page

driver.findElement(By.xpath("//div[contains(@class,'pv-top-card-section__actions')]")).findElement(By.xpath("//span[contains(@class,'default-text') and contains(text(), 'Connect')]")).click();

Explanation : 1. driver.findElement(By.xpath("//div[contains(@class,'pv-top-card-section__actions')]")) this is parent div which has the Inmail and connect button within it, so I am locating the parent div keeping my driver to a limited area.

  1. findElement(By.xpath("//span[contains(@class,'default-text') and contains(text(), 'Connect')]")).click(); fairly straightforward locating the element to be click on based on the class name and to be more accurate giving a context of text() which 'Connect' and clicking on the element.

Let me know if this does not works or in case you are facing any issues.

anshul Gupta
  • 1,182
  • 8
  • 17
  • This solution did not work but I discovered what is happening, the automation is looking for an element that has not yet loaded, because the page loads the elements as the Scroll is triggered, so the element searched is not yet on the page, because the Scroll had not yet been triggered. – Paulo Roberto Jul 21 '17 at 14:59
  • I tried on my local, it did work hoping you are performing operation from user profile page. Anyways, great if you have a solution. :) – anshul Gupta Jul 21 '17 at 15:02
  • Thank you very much for your help, it helped to clarify the ideas lol – Paulo Roberto Jul 21 '17 at 15:04
0

the automation is looking for an element that has not yet loaded, because the page loads the elements as the Scroll is triggered, so, the element searched was not yet on the page, because the Scroll had not yet been triggered.

JavascriptExecutor jsx = (JavascriptExecutor)driver;

//Go down 1000px
jsx.executeScript("window.scrollBy(0,1000)", "");

//up 1000px
jsx.executeScript("window.scrollBy(0,-1000)", "");
Paulo Roberto
  • 1,498
  • 5
  • 19
  • 42