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)
Have you tried to take the XPath direct from a browser? (Chrome example below)
Right click on the element
Inspect Element
Right click on DOM
Select Copy
Click XPath
Paste in By.xpath(TEXT_COPIED)
If isn't that, I guess is the way you are trying to wait the button load.
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.
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.
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)", "");
Related
I'm trying to find and click on a button inside the shadow DOM using Cypress.io.
I've tried various class names, with combinations of get/find and the .shadow() command.
I'm getting this error in Cypress:
"Timed out retrying after 4000ms: Expected the subject to host a
shadow root, but never found it."
Here is some code of the react app I'm testing (with the button id highlighted)
app code screenshot
Below is the latest Cy code I've tried.
cy.get('*[class^="MuiInputBase-input"]').shadow()
cy.find('div[id="search-clear"]')
.invoke('show')
.click()
The button I'm trying to find and click on (it becomes visible upon mouse-over).
button screenshot
Any ideas on how to target this element?
You have to chain .find(selector) after .shadow()
like:
cy.get('.MuiInputBase-input').shadow()
.find('#search-clear')
.invoke('show')
.click()
example in documentation
side note:
Why are you using *[class^="MuiInputBase-input"] and div[id="search-clear"]
instead of .MuiInputBase-input and #search-clear?
when running this command, I'm getting an error:
driver.find_element_by_link_text("Confirm").click()
selenium.common.exceptions.WebDriverException: Message: unknown error: Element ... is not clickable at point (67, 581). Other element would receive the click: `<div class="mfp-container mfp-ajax-holder mfp-s-loading">...</div>`
After searching answers on this issue, I've changed the above code to:
element = driver.find_element_by_link_text("Confirm").click()
driver.execute_script("arguments[0].click();", element)
For the first click it worked and then printed this error:
selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property 'click' of null
The HTML code is:
Confirm
So this worked for me:
driver.find_element_by_link_text("Confirm").send_keys('\n')
Thanks to everybody :)
Try to search for the class:
driver.find_element_by_class("c-button js-close s-vgLeft0_5 c-button--blue").click()
If you look at the error message, you will see that another element is intercepting the click. I don't know for sure without looking at the page but generally it's something like a loader screen, popup, etc. that appears temporarily and then disappears. There is also the hint of one of the classes of the intercepting DIV, mfp-s-loading, that further makes me think it's some sort of loading popup. The problem here is that the script proceeds and tries to click the link faster than the popup loads and unloads. What I typically do in a situation like this is to wait for the popup to be invisible and then click the link.
The HTML of the popup is in the error message,
<div class="mfp-container mfp-ajax-holder mfp-s-loading">...</div>
So you can locate the element using a CSS selector like, div.mfp-s-loading, to wait for it to be invisible and then try your click.
Sometimes using Xpath is easier
Try:
driver.find_element_by_xpath(Xpath).click()
where Xpath should point to the object which you are planning to click
I need to scroll an inner window (i.e. div). I found this web site
How to scroll to an element inside a div?
but this shows how to do Javascript. I am calling the executor but I don't think I can pass values back to java (height, etc). This is my code section. Basically there is a div window with a bunch of elements. Some are hidden on the bottom and do not actually appear in the html unless scrolled to. Once they are scrolled to I believe the will remain there. So I figured I could just scroll a large number and it would not give an error if it were too much, it would just scroll as much as possible.
I scroll to the top and then done, like this:
String sid = rolesScroller.getAttribute("id");
js.executeScript("document.getElementById('" + sid + "').scrollTop -= 1000");
js.executeScript("document.getElementById('" + sid + "').scrollTop += 1000");
waitForXPathVisibility("Scroll", ROLES_SCROLLER_X);
will this be ok or do I need to somehow figure the exact amount to scroll and scroll just by that amount?
I see there is an element.scrollHeight. Does the possible values for scrollTop go from 0 to scrollHeight? Are the units both in pixels?
The elements in the divs are themselves nodes (list values) which can also be expanded creating more elements underneath. Every time I search for a value I have to do the above to make sure everything is in view. The way I have it now works to an extent. But sometimes after scrolling when I try to access a node I get a StaleElementException. However if I do a waitForStaleElement() it sometimes gives an error saying the element did not go stale. Is there a way after executing the javascript that you can make sure all actions have completed so that a stale element won't happen?
To Summarize
When using the javascript executor from Java/Selenium is there a way to pass the javascript variables back to java so they can be used in later jasascript executor commands? (if that example above of -1000 +1000 is OK then this does not matter).
How can you ensure that the javascript command has completed before continuing so when you try to access an element in the scrolled div you will not get a stale element (I tried examining one element in the div, and the div itself).
It seems that the container is dynamically constructed upon scrolling. Try to scroll the last element at the top with scrollIntoView and then wait for a different element at the end:
WebDriverWait wait = new WebDriverWait(driver, 5);
JavascriptExecutor jse = (JavascriptExecutor)driver;
// get the last element
By lastChild = By.cssSelector("#list > div:last-of-type");
WebDriver elem = driver.findElement(lastChild);
// scroll the last element at the top
jse.executeScript("arguments[0].scrollIntoView(true);", elem);
// wait for a new element at the end
wait.until((WebDriver drv) -> !elem.equals(drv.findElement(lastChild)))
Can Selenium use LightSwitch buttons?
At first I had to locate button, the LightSwitch is a button written in CSS, then I will use By.CSSSelector to find.
But after I found the button, check which form it is using if on tap/click etc., I found that it is vclick from Jquery library.
Then I wrote code:
js.ExecuteScript("$('.ui-page-active .msls-footer div div:nth-child(2) span').trigger('vclick')");
But even I find the button using selenium yet it is not being clicked.
Am I missing something here?
Another thing is that, test will pass correctly, but there is no window opened, after clicking.
Application should switch on click to a new screen, but it really doesn't want to do this move.
Screen of button location:
After click expected screen:
Best way to use WebDriverWait instead of Thread.Sleep() as below :-
IWebElement el = (IWebElement)js.ExecuteScript("return $ ('.ui-page-active .msls-footer div div:nth-child(2)')[0]");
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(20));
wait.Until(ExpectedConditions.ElementToBeClickable(el)).Click();
Hope it will work..:)
note: a solution in either Selenium or API wrapper Splinter for Selenium is fine!
I have been having issues interacting with the iframes on Twitter.com using the Splinter API for Python.
For example,
with Browser('firefox', profile_preferences= proxySettings) as browser:
#...login and do other stuff here
browser.find_by_id('global-new-tweet-button').click()
this brings up a pop-up box to type in a tweet.
How do I interact with this new box using Splinter to:
1) fill in a message
2) click "tweet" (submit)
..programmatically of course.
I tried inspecting the element but it doesn't seem to be nested inside of an iframe however it targets an iframe. So I am not sure how to find/interact with the elements in this pop-up.
I tried manually typing in a message then clicking the tweet button programmatically:
browser.find_by_css('.btn.primary-btn.tweet-action.tweet-btn.js-tweet-btn').click()
..but I get the error:
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///var/folders/z1/8rqrglqn2dj8_yj1z2fv5j700000gn/T/tmppRsJvd/extensions/fxdriver#googlecode.com/components/command-processor.js:10092)
at DelayedCommand.prototype.checkPreconditions_ (file:///var/folders/z1/8rqrglqn2dj8_yj1z2fv5j700000gn/T/tmppRsJvd/extensions/fxdriver#googlecode.com/components/command-processor.js:12644)
at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/z1/8rqrglqn2dj8_yj1z2fv5j700000gn/T/tmppRsJvd/extensions/fxdriver#googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///var/folders/z1/8rqrglqn2dj8_yj1z2fv5j700000gn/T/tmppRsJvd/extensions/fxdriver#googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///var/folders/z1/8rqrglqn2dj8_yj1z2fv5j700000gn/T/tmppRsJvd/extensions/fxdriver#googlecode.com/components/command-processor.js:12608)
I strictly want to achieve my goal using Splinter so please do not offer alternatives, I know there are other ways.
Thank you in advance!
You primary problem seems to be that you are treating the results of browser.find_by_xxx as an element object, when in reality it is an element container object (i.e. a list of webdriver elements).
Writing to the field works for me if I reference the element explicitly:
In [51]: elems = browser.find_by_id('tweet-box-global')
In [52]: len(elems)
Out[52]: 1
In [53]: elems[0].fill("Splinter Example")
In [54]:
That will write "Splinter Example" into the field for me.
The button click is failing because your css path is returning a list of three elements, and you are implicitly clicking on the first, hidden element. In my testing, the element you actually want to click on is the second element in the list:
In [26]: elems = browser.find_by_css('.btn.primary-btn.tweet-action.tweet-btn.js-tweet-btn')
In [27]: len(elems)
Out[27]: 3
In [28]: elems[1].click()
In [29]:
When I explicitly click the second element it doesn't throw an error and the button is clicked.
If you add to the css path you can narrow the results to only the button in the visible modal:
In [42]: css_path = "div.modal-tweet-form-container button.btn.primary-btn"
In [43]: elems = browser.find_by_css(css_path)
In [44]: len(elems)
Out[44]: 1
In [45]: elems.click()
In [46]:
Note that no exception was thrown here.