How to Double Click on plain text in webdriver? - selenium

I have moved mouse pointer on to a certain plain text (a word) by this:
Robot robot1 = new Robot();
robot1.mouseMove(430,628); //location of the text/word
Now I just need to double click so that my text/word gets selected. Can you help me on how to do that?
I tried action builder without element ID (as it is a plain text) for doubleclick, which does not work.

This is exactly the case for Selenium Actions:-)
new Actions(driver).doubleClick().build().perform(); //clicks on the current mouse position
However it would be nicer if you could specify the element which you want to click on
new Actions(driver).doubleClick(driver.findElement(By.id("id")).build().perform();

Related

How to add text in pop up window in selenium

I am trying to test a website using selenium(with java). On my website, there is an option to add a new class. When we click on the add new option button, a pop-up window will come and we can enter our new class name. But using selenium I cannot enter alphabets into the pop-up window. I can only enter numbers in that field. The field accept both alphabets and numbers when we enter the data manually. How can I add alphabets to the pop window in selenium?
Here I attach my code below :
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"modal-add\"]")));
driver.findElement(By.xpath("//*[#id=\"class\"]")).sendKeys("LKG");
driver.findElement(By.xpath("/html/body/section/div/div[3]/div/div/form/div[2]/button[2]")).click
screenshot of the field - Class_field_screenshot
As mentioned above you are not able to add alphabets in textfield using sendkeys function. I haven't heard this type of issue before please confirm that either that field accept alphabets or not. if that feild accept alphabets then try using another way like javascript executor to send text as mentioned below
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("$('#id').val("sendtext")");
Can you try Clipboard class for copy and paste string value in class field ? Make sure you click first in class text field and then use paste code. As you say by keyboard you are able to enter alphabet so try to use keys class too.In both cases your keyboard is going to use so may be it helps.

Using sendKeys() without clicking on element in Selenium

I want to input a string then format it using Cleditor. However, when I clicked on the B icon then clicked back on the text area frame to input the text, it lost the bold effect. Then I found out that if I clicked on the B icon, then input the text immediately WITHOUT clicking back on the text area frame, the text would be bold.
Unfortunately I use sendKeys() right after the clicking on B icon code, so it clicks on the text area frame and lose all the bold effect. Here's my code:
clickElement(driver.findElement(By.xpath("//div[#title='boldText']"))); **//click on the bold icon**
**//switch to the textarea frame**
clickElement2();
driver.switchTo().frame(0);
Thread.sleep(1000);
new Actions(driver).sendKeys(driver.findElement(By.xpath(".//*[#class='cleditor-content']")), "abc").perform();
Is there anyway I can input the text without clicking on the text area? Any solution will be appreciated.
As I suggest before in the comments, try to input the text, highlight it and then click the B (bold) button.
Example of that in C# code (should be pretty similar in Java):
var actions = new Actions(WebDriver);
actions.SendKeys(< yourElement >, "your text").KeyDown(Keys.LeftShift).SendKeys(Keys.Home)
.KeyUp(Keys.LeftShift).Build().Perform();
boldButton.Click();

How to select value from Google auto location using Selenium

How to automate this to select particular value even the dropdown list id cannot be inspected. Can anyone help me out on this?
Need to select U.S. 22 Imperial from the list
Please find the HTML snippet
I am unable to proceed more than this. Please help me out!
WebElement location = driver.findElement(By.id("selectbox-city-list2"));
location.sendKeys("us");
You could use sendKeys and send arrow down to select an option. Selecting one by one with the arrow down will highlight the value. You will be able to check the highlighted value using the CSS class of highlighting.
You can use ActionClass
using this you can move your cursor over a specific element based on coordinates and perform a click.
1.So taking the coordinates of that text box.
2.Enter the full value in the text box. ,
3.calculate a very near coordinate to that text box(so that it will be the suggestion) and perform a click.
element = xxxxxxx;
Actions builder = new Actions(driver);
builder.moveToElement(element, x_coord, y_coord).click().build.perform();

Send Tab key 9 times from keyboard to fill specific textbox in Selenium

I need to send Tab key 9 times from keyboard to fill specific textbox in Selenium webdriver.
Is there any snippet for that?
Instead of sending "Tab Keys" 9 times, you shall directly locate the element using xpath or css or id. It's the better thing to do.
However, you can use the below code to press "Tab Keys" 9 times using Robot class:
for(int i=1;i<=9;i++){
Robot r = new Robot();
r.keyPress(KeyEvent.VK_TAB);
r.keyRelease(KeyEvent.VK_TAB);
System.out.println("Tabbed "+i+" time.");
}

Selenium, using xpath to click a button with a similar content

I want to click a button that contains "Add" as text.
ie:
driver.find_element_by_xpath("(//a[contains(text(),'Add')])").click()
however it's not practical to do this:
driver.find_element_by_xpath("(//a[contains(text(),'Add')])[1]").click()
It would be fine, except the page has a button with text "Add User", and it clicks that instead. Is there a way to only click it if it is EXACTLY "Add" and not just contains "Add"?
You can also try :
driver.find_element_by_link_text("Add").click()
link text will match links who's text equals Add and it doesn't match text which contains Add as part of it.
This should work for you:
driver.find_element_by_xpath("//a[text()='Add']").click()
You should change your xpath to require an exact match instead of a partial match.
driver.find_element_by_xpath("//a[text()='Add']").click()
Using text()= requires the text on the button to be equal to 'Add' and will not find other buttons that happen to contain the text 'Add'