Can Selenium use buttons from LightSwitch librarz? - javascript

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..:)

Related

Click element with selenium with java

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)", "");

How to click on Javascript button in Selenium with no ID (Java)?

I'm setting up a program in Java with Selenium.
At the start of the program, a Chrome extension I'm using with the program loads up with the Chrome instance.
Chrome then navigates to that page, selects all the boxes, and is supposed to click a button on the page that appears because of the extension.
So I am trying to click that button, but it's a Javascript button that comes on the page through the extension. There is no ID that I can use explicitly though.
When I inspect the element, all I see is this:
<a href="javascript:void(0);" class="selected button-task"
style="width: 140px; margin-left: 5px; height: 23px;">
<img src="websiteimage.png here" width="20px">Selected Task</a>
Unlike the other stuff I can click on, there is no type (checkboxes, button, etc) or a specific ID I can seek out. But it is important I click this button. What should I do?
I get this error when I use this:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector:
Unable to locate an element with the xpath expression //a[contains#class,'selected'] and contains(#class, 'repost-selected button-task') and contains(text(), 'Repost Selected') because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document':
The string '//a[contains#class,'selected'] and contains(#class, 'repost-selected button-task') and contains(text(), 'Repost Selected')' is not a valid XPath expression.
Thanks!
But it is important I click this button. What should I do?
You can click this button using following approaches :-
WebDriverWait wait = new WebDriverWait(driver,10);
using By.cssSelector() :-
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.selected.button-task"))).click();
using By.linkText() :-
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Selected Task"))).click();
using By.partialLinkText() :-
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Selected Task"))).click();
using By.xpath() :-
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//a[normalize-space()='Selected Task']"))).click();
Try This:
driver.findElement(By.xpath("//a[contains#class,'selected'] and contains(#class, 'button-task') and contains(text(), 'Selected Task')")).click()
I figured out how to solve it:
WebElement extensionBox = driver.findElement(By.xpath(".//a[normalize-space()='Selected Task']"));
Actions actionsTwo = new Actions(driver);
JavascriptExecutor jseTwo = (JavascriptExecutor) driver;
actionsTwo.moveToElement(extensionBox).click();
jseTwo.executeScript("arguments[0].click()", extensionBox);
The other answers weren't working because they either wouldn't find the object on the page, or would give compile errors

Unable to click the Link in selenium webdriver

I am trying to Click on Link. But it is showing Element Not found Message. Here is my HTML Code:
<a id="expTo" class="formblue_link padRight10
exportLinkActive" style="display: block; margin-left: -50px; margin-bottom: -20px;" href="javascript:;"> Export To</a>
My code is:
`driver.findElement(By.linkText("Export To")).click();`
It's recomanded to not try to find element by button or item text. This can be easy changed while page is still in develop so I would suggest to use click by id instead of by text
driver.findElement(By.id("expTo")).click();
There is also alternative to click by css for example:
driver.findElement(By.cssSelector(".css-class-name"));
All others selectors can be found here
You should try using WebDriverWait to wait until element visible and clickable as below :-
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("expTo"))).click();
Note :- Before going to find this element, make sure it is not inside any frame/iframe. If it exists inside any frame/iframe, you need to switch that frame/iframe before finding it as :-
driver.switchTo().frame("frame/iframe name or id");

Interacting with iFrames Using Splinter/Selenium [Python]

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.

Onclick doesn't work normally and move button to the next line doesn't work

I use jsfiddle run it and the source code is http://jsfiddle.net/eVyjC/12/
I want to add a button to start and stop number changing. I use a div containing
six spans, each has a number.
Then I use createElement to create a input button and set onclick to run the user-defined
function setInterval(keepMove) but it doesn't work and no any error come out?
And then I test by use alert, to my surprise it run when load the page without click the
button?
My questions are:
1.Why the onclick run first when loading and how to let it run by clicking the button as I expect?
2.I try to add a br to the last span to move the button to the next line, but it doesn't work. What's do I wrong and how can I fix it?
Thx you for reading my problem, I have tried to figure it serveral days and try many methods but don't work.(eg document.write html tag or createElement and Childappend )
1: Instead of:
buttonNode.onclick= alert('WhyIFirstRunQQ');
Do:
buttonNode.onclick= function(){ alert('WhyIFirstRunQQ'); }
That should do the trick.
2: Use document.write("<br>");
I strongly recommend using a javascript library such as jQuery instead of doing all this by hand.

Categories