I am identifying a clickable WebElement by CssSelector. An example code I am using is:
String selectorString = "a.some-text.item";
WebElement we = driver.findElement(By.cssSelector(selectorString));
we.click();
The problem is, that after the item has been clicked its CssSelector changes to a.some-text.item.clicked. I can still securely identify it by using above selectorString variable. However, I only want to click it if it has not been clicked before.
How can I determine the items status, i.e. whether its cssSelector name is a.some-text.item or a.some-text.item.clicked?
I only want to click it if it has not been clicked before.
Try using :not(selector) function of cssSelector to determine only unclicked element :-
String selectorString = "a.some-text.item:not(.clicked)";
WebElement we = driver.findElement(By.cssSelector(selectorString));
we.click();
Edited :- If you want to just determine whether element has contains clicked class or not after clicking then try as below :-
String selectorString = "a.some-text.item";
driver.findElement(By.cssSelector(selectorString)).click();
//Now verify element clicked or not
WebElement we = driver.findElement(By.cssSelector(selectorString));
String msg = (we.getAttribute("class").contains("clicked")) ? "element clicked" : "element not clicked";
System.out.println(msg);
Related
I'm able to navigate this page [here][1] and enter this website [here][2](TOKEN CREATED FOR EACH DEMO FOR THIS LINK)
I'm also able to extract the value from the round count using this xpath here #class,'coefficient'
I can locate the input element on the left hand side but only one text value is deleted. I want to delete all values and enter 50.
It also seems like I can locate the left hand side button because I'm not getting any exceptions or errors but the button is not clickable.
The below two lines:
firstInput.sendKeys(Keys.CONTROL + "a");
firstInput.sendKeys(Keys.DELETE);
is basically to clear the input field, since .clear() is not working as expected in this case, we'd have to do CTRL+a and then delete
A full code will look like this:
driver.manage().window().maximize();
driver.get("https://www.spribe.co/games/aviator");
WebDriverWait wait = new WebDriverWait(driver, 30);
try {
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),' Got it')]"))).click();
}
catch(Exception e){
e.printStackTrace();
}
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Play Demo ')]"))).click();
String originalWinHandle = driver.getWindowHandle();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(text(),'Yes')]"))).click();
List<String> allHandles = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(allHandles.get(1));
WebElement firstInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label[text()='BET']/ancestor::div[contains(#class,'feature')]/descendant::input")));
firstInput.sendKeys(Keys.CONTROL + "a");
firstInput.sendKeys(Keys.DELETE);
firstInput.sendKeys("20");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[text()='BET']/.."))).click();
I am trying to automate the flight booking of http://www.mercurytravels.co.in using Selenium+java.
The issue that I am facing is that I'm unable to click on the ">" (next button) of the "Date of Return" date field in the Book Flights Online page.
Getting error message that --> Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable
The code that I've written is:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
//Retrieving current year value
String currentReturnMonthYear = driver.findElement(By.xpath("//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[2]")).getAttribute("innerHTML");
driver.findElement(By.xpath("//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")).click();
Request any inputs on issue that I'm facing?
org.openqa.selenium.ElementNotInteractableException Indicates that a click could not be properly executed because the target element was obscured in some way. ElementNotInteractableException is Thrown to indicate that although an element is present on the DOM, it is not in a state that can be interacted with.
Using Explicit wait:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
WebElement next = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(
"//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")));
next.click();
Using Actions:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
WebElement next = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(
"//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")));
Actions action = new Actions(driver);
action.moveToElement(next).click().build().perform();
Using JavascriptExecutor:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
WebElement next = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(
"//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")));
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
javascriptExecutor.executeScript("arguments[0].click();", next);
I have problem to automate this drop down using selenium web driver using Java
This is the link - Go to 5th drop down named as Github users (fetch. js)
I am not able to enter the data into search field.
I am using send keys after perform click but it throws an exception like this " element is not interact able"
Steps I follow
driver.findElement(By.xpath("xapth")).click
drop down opens with no options because it is searchable and options are coming dynamically after entering key word into the search field.
driver.findElement(By.xpath("xapth")).sendkeys("Test");
Sendkeys are not working in this case because of drop down closed when perform send keys action.
<div class="Select-placeholder">Select...</div>
Below is the code which is working.
Please do optimize the code by removing thread.Sleep and putting some meaningful waits as per your requirement.
driver.Navigate().GoToUrl("https://jedwatson.github.io/react-select/");
IWebElement element1 = driver.FindElement(By.XPath("//span[#id='react-select-6--value']"));
IWebElement element2 = driver.FindElement(By.XPath("//span[#id='react-select-6--value']/div[2]/input[1]")) ;
element1.Click();
Thread.Sleep(2000);
element2.SendKeys("Test");
Thread.Sleep(1000);
element2.SendKeys(Keys.Tab);
Please note that element2 gets activated once you click on element1.
Try the following code:
public void typeAndSelect() {
WebElement searchBox = driver.findElement(By.xpath("//div[#class='section'][5]//div[#class='Select-control']"));
searchBox.click();
WebElement inputField = driver.findElement(By.xpath("//div[#class='section'][5]//input[#role='combobox']"));
inputField.clear();
String searchWord = "test";
inputField.sendKeys(searchWord);
WebElement selectDropdown = driver.findElement(By.xpath("//div[#class='Select-menu-outer']//div[#role='option'][text()='" + searchWord +"']"));
// wait for search results.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(selectDropdown)).click();
}
Correct the following xpath part
"//div[#class='section'][5]"
to your implementation of the dropdown
I have logged into a website page using automation code (Selenium) but now there are fields in which data needs to be entered
But how to do this using Selenium?
How to write the code for it?
Use sendKeys method.
driver.findElement(By.id("InputBox_ID")).sendKeys("Test data");
Use following lines of code to insert value into text.
WebElement Element1 = driver.findElementByName("abc");
Element1.sendKeys("value that you want to enter.");
When searching for a field to fill in, right click on its element in the web page and click "Inspect Element". The ID should be highlighted in the popup menu, and that is going to be the ID You specify into the "By.xpath()" Method from seleniums API.
Here is an example on how to fill in fields for Gmail.
System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver();
driver.get(Links.Register_Gmail);
driver.findElement(By.xpath("//*[#id='FirstName']")).sendKeys(firstname);
driver.findElement(By.xpath("//*[#id='LastName']")).sendKeys(lastname);
driver.findElement(By.xpath("//*[#id='GmailAddress']")).sendKeys(username);
driver.findElement(By.xpath("//*[#id='Passwd']")).sendKeys(password);
driver.findElement(By.xpath("//*[#id='PasswdAgain']")).sendKeys(password);
driver.findElement(By.xpath("//*[#id='BirthMonth']")).click();
driver.findElement(By.xpath("//*[#id=':" + random(1, 9) + "']")).click();
driver.findElement(By.xpath("//*[#id='BirthDay']")).sendKeys("" + random(1, 27));
driver.findElement(By.xpath("//*[#id='BirthYear']")).sendKeys("" + random(1950, 1990));
driver.findElement(By.xpath("//*[#id='Gender']")).click();
driver.findElement(By.xpath("//*[#id=':f']")).click();
driver.findElement(By.xpath("//*[#id='submitbutton']")).click();
driver.findElement(By.xpath("//*[#id='tos-scroll-button']")).click();
driver.findElement(By.xpath("//*[#id='tos-scroll-button']")).click();
driver.findElement(By.xpath("//*[#id='tos-scroll-button']")).click();
driver.findElement(By.xpath("//*[#id='iagreebutton']")).click();
you can do it simply by just right click on the field that you want to fill and click inspect, if you are using firefox then it is quiet simple by using firepath but if you are using chrome then simply right click on the field, click on inspect element and find a unique id of that field and put it inside these below lines
driver.findelement(By.id("")).sendelement(""),
you can use different field than id if you find something else unique.
You can use the below method and call it wherever you want to enter text
public static void enterTextInput(WebElement element, String value) throws InterruptedException{
String val = value;
element.clear();
Thread.sleep(1000);
for (int i = 0; i < val.length(); i++){
char c = val.charAt(i);
String str = new StringBuilder().append(c).toString();
element.sendKeys(str);
}
Thread.sleep(1500);
}
Driver.findElement(By.xpath("//*[#id='client']")).sendKeys("Ho");
Driver.manage().timeouts().implicitlyWait(1,TimeUnit.MINUTES);
WebElement dropdown = (new WebDriverWait(Driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='client']")));
Driver.findElement(By.xpath("//*[#id='collapseClientInfo']/div/form/div[3]/div[2]/ul/li[1]/a")).sendKeys(Keys.ENTER);
Could you please help me to select auto populate value from drop down list:
We've Client textbox which is an auto-populate box.
When I enter "ho" text in the client field, it shows me the drop down which has values related to my entered text i.e. ho, then I have to select those values which are available under list.
In above code I've tried with Press Enter but unable to select the value.
Could you please check the above code and help me out for the same?
You should try as below :-
WebDriverWait wait = new WebDriverWait(Driver, 60);
//wait until loader invisible
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loaderDiv")));
//this sleep is required because after invisibility of loader focus goes to first input which is Requisition Number
//If you are filling form from first input no need to for this sleep
//if you want to input directly to client field need to sleep to avoid focus first
Thread.sleep(3000);
//Now find the client input and set value
WebElement client = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("client")));
client.sendKeys("Ho");
//Now find all the showing option
List<WebElement> dropdownOptions = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("ul.dropdown-menu a")));
//Now select the first option
dropdownOptions.get(0).click();
Below approach might be helpful:
// Enter text in auto complete text box
driver.findElement(By.xpath("//*[#id='client']")).sendKeys("Ho");
// Wait for options to display
Thread.sleep(5000);
// Option to select
String optionToSelect = "Honda";
Boolean isOptionSelected = Boolean.FALSE;
// Get the options displayed
List<WebElement> options = driver.findElements(By
.cssSelector("ul.dropdown-menu a"));
// Select option
for (WebElement webElement : options) {
if (webElement.getText().equalsIgnoreCase(optionToSelect)) {
webElement.click();
isOptionSelected = Boolean.TRUE;
}
}
if (isOptionSelected) {
// Option is selected
} else {
// Expected option is not displayed. Fail the script
}
Try this:
Select drpCountry = new Select(driver.findElement(By.id("searchOptions")));
drpCountry.selectByVisibleText("By author");