Locating CK Editor and Sending text to it in Selenium Webdriver - java

Hi I am trying to locate CK editor for my project through Selenium Webdriver code(Java). But Whenever I try to use SendKeys() method it is not working for me.
Below is the screenshot of CK Editor and HTML code.
And below is the code,
if(driver.findElement(By.cssSelector("iframe#scayt_8")).isEnabled())
{
WebElement iframe = driver.findElement(By.cssSelector("iframe#scayt_8"));
System.out.println("Frame Enabled");
if(driver.findElement(By.xpath("//iframe[#id = 'scayt_8']")).isDisplayed())
{
System.out.println("Frame Displayed");
driver.switchTo().frame(iframe);
iframe.clear();
System.out.println("Clicking frame");
iframe.click();
iframe.sendKeys("Hello!!");
}
}
Please help me to locate CK Editor and to Send text to it.

You probably need to switch to the inline frame to locate it.
WebElement editorFrame = driver.findElement(By.id("scayt_8"));
driver.switchTo().frame(editorFrame);
WebElement body = driver.findElement(By.tagName("body"));
body.clear();
body.sendKeys("some text");
We provide techniques for working with editors in chapter 3 of our book Selenium WebDriver In Practice.

I think iframe is being searched based on cssSelector but i think it is supposed to be based on id? which is scayt_8. Can you try with following code to fetch iframe instead of cssSelector:
driver.FindElement(By.TagName("iframe"))

Once you switch to iframe, try to locate webelement by paragraph tag name inside iframe, something like below:-
WebElement body=driver.findElement(By.tagName("p"));
Then try to send keys using this webelement:
body.sendKeys("Hello!!");

WebElement iframe = driver.findElement(By.tagName("iframe")); driver.switchTo().frame(iframe);
WebElement tinymce = driver.findElement(By.tagName("body"));
tinymce.clear();
tinymce.sendKeys("Hello, ckeditor!");;
This will help you to send text in CKeditor. Try this. It will work

Related

xPath Text Contains Selenium Web Driver

I'm trying to select an element based on its text contents. I am using XPath to achieve this.
I am just puzzled as this should work?
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
I'll even copy the HTML code:
<div class="linkWrap noCount">Notifications <span class="count _5wk0 hidden_elem uiSideNavCountText">(<span class="countValue fsm">0</span><span class="maxCountIndicator"></span>)</span></div>
The div element has the words "Notifications" inside it. So why doesn't it work.
Go to this page on Facebook: https://www.facebook.com/settings
Use this chrome extension to highlight any area via xPath.
You have a space before the word Notifications:
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
You should also add a wait for element before trying to find the element:
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(), 'Notifications')]"));
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
I found the issue with the help of some amazing people in this community.
Ok, so my element was in an iFrame.
In order to access the element, I must first access the iFrame
WebElement iframe = obj.driver.findElement(By.xpath("//iframe[#tabindex='-1']"));
obj.driver.switchTo().frame(iframe);

Can not take correct identifier of WebElement (Sign in at iCloud.com)

I have trouble identifying 'sign in with Apple ID' element (at iclod.com page).
This is what I'm using now:
WebElement username = driver.findElement(By.xpath("//[#id=\"account_name_text_field\"]");
username.sendKeys("my_email#icloud.com");
Also, I tried to use CSS created by Chropath and Ranorex, still not working.
What I'm doing wrong?
Path to needed Element
Please check below solution. Iframe is associated with your web page and you need to switch to iframe before you interact with web element input box.
driver = webdriver.Chrome(executable_path=r" path of chromedriver.exe")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
driver.get("https://www.icloud.com")
wait.until(EC.presence_of_element_located((By.ID, "auth-frame")))
driver.switch_to.frame("auth-frame")
inputBox = wait.until(EC.element_to_be_clickable((By.ID, "account_name_text_field")))
inputBox.send_keys("your test")
# switch back to main window
driver.switch_to.default_content()
output:
The element is inside the iframe, you need switch first.
You can use .frameToBeAvailableAndSwitchToIt:
driver.get("https://www.icloud.com/");
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("auth-frame")));
WebElement username = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("account_name_text_field")));
username.sendKeys("my_email#icloud.com");
Add WebDriverWait, the web loading is rather long.
Iframes can be tricky. You have to change focus to the frame before accessing that text box.
Use switchTo().frame ();
It can take the following parameters :
switchTo().frame(int frame number): Defining the frame index number,
the Driver will switch to that specific frame
switchTo().frame(string
frameNameOrId): Defining the frame element or Id, the Driver will
switch to that specific frame
switchTo().frame(WebElement
frameElement): Defining the frame web element, the Driver will
switch to that specific frame
WebDriver driver = new FirefoxDriver();
waitdriver = new WebDriverWait(driver , 10);
driver.get("https://www.icloud.com/");
driver.switchTo().frame("auth-frame");
waitdriver.until(ExpectedConditions.presenceOfElementLocated( By.xpath("//*[#id=\"account_name_text_field\"]")));
WebElement textbox=driver.findElement(By.xpath("//*[#id=\"account_name_text_field\"]"));
textbox.click();
textbox.sendKeys("HelloWorld#gmail.com");
1) The xpath pattern which you have written is wrong.
2) You can always verify the xpath or cssSelector in ChroPath before using it in your script.
3) If the element is inside iframe then you must have got the iframe xpath as well as the element xpath in ChroPath. Please use them.
Please follow this video tutorial to make the best use of this iframe feature of ChroPath.

How to press Page Down key in Selenium to scroll down a webpage through Java?

I want to scroll down my web page. What Java command should I use in Selenium?
Scrolling down a web page is not a valid usecase which can be validated perhaps you want to scroll down to bring a WebElement within the Viewport to interact with it. To achieve that you can use the executeScript() method as follows :
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", element);
Use the below code and try,
JavascriptExecutor js = (JavascriptExecutor) driver;
// Launch the application
driver.get("http://demo.guru99.com/test/guru99home/");
//This will scroll the web page till end.
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Please refer the code below :
1) Using Action class****
WebDriver driver = new ChromeDriver();
//Creating an object 'action'
Actions action = new Actions(driver);
//open SoftwareTestingMaterial.com
driver.get(Site URL); // Give site URL as name of the web page
//sleep for 3secs to load the page
Thread.sleep(3000);
//SCROLL DOWN
action.sendKeys(Keys.PAGE_DOWN).build().perform();
Thread.sleep(3000);
//SCROLL UP
action.sendKeys(Keys.PAGE_UP).build().perform();

click on a button with JQuery

There is somthing wrong with the page I want to test.
My first try:
When I clicked manually on a button, then I will be forwarded normally on the next page.
When I tried to click on the same button with selenium, then I get an error page "Sorry...something gone wrong...blabla". I think this problem can only solve the developer team of the page.
By book = By.cssSelector("#button\\.buchung\\.continue");
//By book = By.cssSelector("button.buchung.continue");
//By book = By.xpath("//*[#id='button.buchung.continue']");
WebElement element= ConfigClass.driver.findElement(book);
element.click();
But I want to try a workaround:
I clicked on the same button with JQuery.
I opened my chrome console and execute the button with:
jQuery('#button\\.buchung\\.continue').click()
How can I execute this JQuery expression in my selenium code?
I tried this, but without success:
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("jQuery('#button\\.buchung\\.continue').click()");
Use $
je.executeScript("$('#button\\.buchung\\.continue').click()");
jQuery("selector") will return you a list.
I think you have to call click() on the element at index 0 (Assuming exactly one element satisfies the selector)
Code:
je.executeScript("jQuery('#button\\.buchung\\.continue')[0].click()");
You were pretty close. If the cssSelector is uniquely identifying the WebElement you can use the following code block :
By book = By.cssSelector("#button\\.buchung\\.continue");
WebElement element= ConfigClass.driver.findElement(book);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

Unable to find draggable web element in selenium webdriver(while other user is able to do so with same code)

I'm trying to test drag and drop functionality using selenium webdriver while writing test scripts using java in eclipse.I'm unable to find the web element that I want to drag while my other friend is able to find the element with same code.
Below is the code we both have used.
WebElement draggable = driver.findElement(By.id("draggable"));
I'm using site www.way2automation.com/demo.html and the page i'm trying to find webelement on, is http://way2automation.com/way2auto_jquery/draggable.php
Please suggest something to find the webelement.
Try this below code, using Action Class.
WebElement from = driver.findElement(By.id("drag"));
WebElement to = driver.findElement(By.id("drop"));
Actions act = new Actions(driver);
act.clickAndHold(from).build().perform();
Thread.sleep(3000);
act.clickAndHold(to).moveToElement(to).release(to).build().perform();
Thread.sleep(2000);
WebElements were not being identified even with right locators because these webElements were under an iFrame so First we need to switch to the iFrame before performing drag and drop. And then we also need to check for the availability of SourceElement and DestinationElements. Below code is working fine for this problem.
//Wait for the frame to be available and switch to it
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".demo-frame")));
WebElement Sourcelocator = driver.findElement(By.cssSelector(".ui-draggable"));
WebElement Destinationlocator = driver.findElement(By.cssSelector(".ui-droppable"));
builder.dragAndDrop(Sourcelocator,Destinationlocator).build().perform();
String actualText=driver.findElement(By.cssSelector("#droppable>p")).getText();
Assert.assertEquals(actualText, "Dropped!");

Categories