How to automate shadow-root (open) in selenium web driver - selenium

I have an question about shadow-root (open). I tried so many ways automate it. But most ways are not worked for me. However, I found a way. But, some buttons are not automate at that method. Below is how I wrote the script.
WebElement element3 = driver.findElement(By.cssSelector("macroponent-f51912f4c700201072b211d4d8c26010")).getShadowRoot()
.findElement(By.cssSelector("sn-polaris-layout")).getShadowRoot()
.findElement(By.cssSelector("sn-impersonation")).getShadowRoot()
.findElement(By.cssSelector("now-modal")).getShadowRoot()
.findElement(By.cssSelector("now-button")).getShadowRoot()
.findElement(By.cssSelector(".now-button.-primary.-md"));
Thread.sleep(3000);
element3.click();
enter image description here
This is how I automate shadow-root(open)
enter image description here
But, when a button I automated like this, It is not automated. Because the button id is not identified. It changes time to time. What is the reason and is there any other way to do this
enter image description here
This is the script I wrote to automate the button.

Related

How to copy all content in a webpage using selenium in headless mode?

Below I describe the issue I need help with.
Description
I wan't to simple copy content from URL: https://www.di.se/bors/large-cap/ to my Windows clipboard. I scroll down a bit on webpage (to get all content) and mark all before copy.
I want to this in selenium headless mode.
Problem
When I scroll down a little on the page and mark all and then copy the content, the clipboard does not contain the content I copied in headless mode in selenium. Important to know that this working as expected in normal selenium mode.
I have tried with three different browsers: Chrome, Edge and with Firefox. I have also tried with different ways to make the nessecary key strokes (sendKey, Actions and with the Robot class). See code example of the different ways below. Regardles what I'm doing the content is never copied to the clipboard.
Examples:
((JavascriptExecutor) driver).executeScript("scroll(0,400)");
driver.findElement(By.cssSelector("body")).sendKeys(Keys.chord(Keys.CONTROL, "a"));
driver.findElement(By.cssSelector("body")).sendKeys(Keys.chord(Keys.CONTROL, "c"));
Actions builder = new Actions(driver);
Action selectAll=builder.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).build();
Action copy=builder.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).build();
selectAll.perform();
copy.perform();
iRobot.keyPress(KeyEvent.VK_CONTROL);
iRobot.keyPress(KeyEvent.VK_A);
iRobot.keyRelease(KeyEvent.VK_A);
iRobot.keyRelease(KeyEvent.VK_CONTROL);
iRobot.keyPress(KeyEvent.VK_CONTROL);
iRobot.keyPress(KeyEvent.VK_C);
iRobot.keyRelease(KeyEvent.VK_C);
iRobot.keyRelease(KeyEvent.VK_CONTROL);
Question
Is there a way to copy the content to clipboard or can some workaround help?
From what I can find, it seems there is no way to access the clipboard in headless mode. I can't find anything definitive but this is as close as I can get to proof that it can't be done: https://groups.google.com/g/selenium-users/c/-nxTX4eTbwA/m/wQY_HlZQBAAJ

Is the Selenium click() recognized as human or automation?

My scenario which produces the question goes something like below:
I enter a webpage via normal means, next I press on a button, to start a HTML5 application on this webpage, this application is inside an iFrame. On application start I'm being prompted to either turn the sound on or off. At this point there are two possible outcomes:
1. When I answer this prompt manually, new buttons appear in the application window, as expected.
2. When I answer this prompt through automation via Appium, new buttons do not appear.
Now to the question:
To answer the prompt I use the click() method from Selenium. Is it possible that this click() is not considered to be executed by a human and therefore doesn't trigger necessary things? And since I don't have access to the source of the application can I force the Selenium click() to look exactly like a human click?
Here is the code I use to execute the mentioned click:
//Application loading up, hence the sleep
Thread.sleep(5000);
AppiumTestBase.getDriver().switchTo().frame("e_iframe");
Thread.sleep(5000);
WebElement soundOff = AppiumTestBase.getDriver().findElement(By.id("soundOff"));
AppiumTestBase.getStandardWaitTime().until(elementToBeClickable(soundOff));
soundOff.click();
The program is able to find and switch in to the iFrame, there are no cross-origin issues either. The AppiumTestBase is just there for initializing the driver, setup capabilities etc. I also tried clicking the element via Actions and JavaScript, but there was no change in behavior.
In C# a workaround I've found to actually take control of the mouse and move it/click with it is to use "Microsoft.VisualStudio.TestTools.UITesting" for the Keyboard/Mouse libraries. From there, you can tell it "Mouse.Click(new Point(X, Y));"and it will move your mouse to that location and click.
Sample Code:
using Microsoft.VisualStudio.TestTools.UITesting;
var soundOff = AppiumTestBase.getDriver().findElement(By.id("soundOff"));
Mouse.Click(new Point(soundOff.Bounds.X, soundOff.Bounds.Y));

How to record test cases with robotframework?

I recently started using robotframework with the Selenium2Library. I haven't tested using Selenium before, but I know it is possible to record tests using Selenium. In RobotFramework, it says, "it is not possible". I mean even for a simple login test, I need to write the test, specifying the id of username, password and submit button.
However, is there any way by which these tests can be recorded using robotframework? such as clicking on a text box, entering a string and then clicking on submit button etc., and automatically generate the test case source code instead of having me to write the test cases. Is this possible with robotframework or any external library that it supports?
robotframework wasn't designed to be a record-and-play tool, and has nothing built-in to support that.
There was someone who wrote a selenium IDE plugin that would generate robot keywords, but that was years ago. The github repository is here: https://github.com/denschu/selenium-ide-format-robotframework
The code hasn't been touched since 2012, so I doubt it's of much use.
Using this Firefox add-on, FireRobot we can generate most of the code and also using this you can select the elements on the screen and get related code suggestions on right click like
Wait Until Element Is Visible
Click Element
and all operations to perform on the selected element.

Automating "Browse" button event in Selenium

I need to automate the "Browse" button click from Selenium.
For this, I have tried
driver.findElement(By.xpath("//*[#id=\"dnn_ctr383_View_filename\"]")).click();
and
driver.findElement(By.cssSelector("Css path")).click();
Both gives me org.openqa.selenium.NoSuchElementException: Unable to locate element: exception.
I have seen this link here where the author suggest to use AutoIT, but in step 2, the script, the author has created is for IE. Can someone please suggest, how I can automate the "Browse" button click in firefox?
Any help is much appreciated.
Directly send the file path to the id, like so
driver.findElement(By.id("dnn_ctr383_View_filename")).sendKeys("C:\\path\\to\\file");
The above step is the answer for your first two steps
Click on Browse
Select a file to upload
For the third step(click upload), looking at the screen capture I do not see any button which says "Upload". So just click "Save" and I assume that your file will successfully get uploaded.
There are two things that u need to consider here:
Clicking the browser button: Usually handled by an alert or popup, if the driver is not able to find the element by xpath(which u have acquired from firebug or chrome's inspect element) you should consider looking for iframes in the page source. If an element is in a different frame altogether , u need to switch frames in order to find the element like this
WebElement frame = driver.findElementById("name_of_iframe");
driver.switchTo().frame(fr);
now you can find your element using xpath or css selector like u did. Once completed u can move out of the frame by:- driver.switchTo().defaultContent();
Uploading the file from desktop: Since selenium works only on the html in the browser session u invoked from the driver, it can't perform operations on your desktop.Once you are able to click the browser button u can use Auto it(works only on windows) or Sikuli(works with mac, linux, windows and even android) to simulate your events based on the position of your upload button.
Hope this helps

Element link not found in Selenium

I am executing a simple recorded script in Selenium , in which i just search a page in Google and then click it , but when Run that recorded script it is giving me the "Error [error] Element link not found".
And following is the Script which i am running,any help would be very useful.
New Test
New Test
open
/
type
q
Bill Gates
click
link=Bill Gates: Chairman
click
link=Bill Gates: Chairman
I got the same error message while going through the tutorials online. I found out that the scripts were running too fast for FF to load the links. If you slow down the playback by moving the green button on the Fast-Slow bar to somewhere in the middle the scripts run without any errors.
I guess I know the answer. It might be bcz of any javascript error(you can see that on the status bar at the
Right corner(IE)) or that link element might not be placed correctly in the box(box model).
Here you can use
Selenium.focus("link=text")
:selenium.keyPressNative("10") . It presses the enter key.but while selenium is executing this statement
Do not change the focus from your AUT (don't touch the mouse).
In IDE ,just paste the above statement on click().
Well, I don't think you have to slow the speed down. You know the problem is caused by clicking on the link before it is available, so you could add an extra command waitForVisible (e.g. link=Bill Gates: Chairman) before the clickAndWait command.
Im assuming you are using Google instant as there is no "click search button" command, the link is probably not preset because selenium is not waiting for Google instant to respond or the javascript to render the link. If you manually add in a click command to search it should solve it.
My solution is almost the same as Cuongs. Though I change the speed by using a command and change it back afterwards.
Before you click the link, you can set the speed to slow:
Command: setSpeed
Target: 3000
You can change the speed afterwards again to ~1000
I also had same problem. I did two things which fixed the problem. I changed the speed in Selenium IDE from fast to slow (located at top left of IDE), and I minimized the IDE after hitting play button. I guess for people using only one monitor, minimizing Selenium IDE would help the test find the thing it is looking for; for ppl with two screens, opening Selenium IDE in one screen, and opening browser in another screen would be best.
Info: Installing this plug-in (Selenium IDE: Flow Control) fixed lots of unexpected errors for me while playing with Selenium IDE:-
https://addons.mozilla.org/en-US/firefox/addon/flow-control/?src=dp-dl-othersby