How to click on a specific text of search text box results in selenium WebDriver. In my case search text box is "School".
I'm sending keys "RGSchool1" in the text box "School" and then I want to click on "RGScool" when it shows up as results under the text box.
I tried all below approached by it's throwing "org.openqa.selenium.NoSuchElementException"
Enter text and tab out
Enter text and send enter key
Absolute path - /html1/body1/div[7]/ul1/li1/div1/span1
Relative path - //span[#class='select2-match']
HTML body:
<div class="select2-result-label" style="" xpath="1">
<span class="select2-match">RGSchoo</span>
l1 [rgschool1]</div>
Code:
//Finding elements
#FindBy(id = "s2id_User_OrgId")
public WebElement clickJurisdiction;
#FindBy(xpath = "/html[1]/body[1]/div[6]/div[1]/input[1]")
public WebElement keyInJurisdiction;
#FindBy(xpath = "//div[#id='s2id_User_OrgUnitId']//a[#class='select2- choice']")
public WebElement clickSchool;
#FindBy(xpath = "/html[1]/body[1]/div[7]/div[1]/input[1]")
public WebElement keyInSchool;
#FindBy(xpath = "/html[1]/body[1]/div[7]/ul[1]/li[1]/div[1]")
public WebElement schoolSearchResult2;
Call method:
public void enterNewUserData() {
SeleniumTestHelper.enterText(firstName, Config.getProperty("FirstName"));
SeleniumTestHelper.enterText(middleName, Config.getProperty("MiddleName"));
SeleniumTestHelper.enterText(lastName, Config.getProperty("LastName"));
SeleniumTestHelper.enterText(preferredName, Config.getProperty("PreferredName"));
SeleniumTestHelper.clickOnButton(clickJurisdiction);
SeleniumTestHelper.enterText(keyInJurisdiction, Config.getProperty("Jurisdiction"));
SeleniumTestHelper.enter(keyInJurisdiction);
SeleniumTestHelper.clickOnButton(clickSchool);
SeleniumTestHelper.enterText(keyInSchool, Config.getProperty("School"));
SeleniumTestHelper.clickOnButton(schoolSearchResult2); // It fails here
Please help me to find a solution. I'm new to this kind of scenario.
Please see the below-attached screenshot.
Screenshot before entering data:
Screenshot after entering data
To locate the element with text as RGSchool1 you can use the following Locator Strategy:
Using cssSelector:
#FindBy(cssSelector = "ul.select2-results div.select2-result-label > span.select2-match")
public WebElement schoolSearchResult2;
Using xpath:
#FindBy(xpath = "//ul[#class='select2-results']//div[#class='select2-result-label']/span[#class='select2-match']")
public WebElement schoolSearchResult2;
Related
/I want to select Mumbai as source and Delhi as destination from autosuggestion on cleartrip website. I have written below code. Here source is getting handled properly but on destination, autosuggestion list is displayed but nothing get selected from the list. Could someone please help me out/
String baseurl = "https://www.cleartrip.com/";
driver.get(baseurl);
String title = driver.getTitle();
System.out.println(title);
WebDriverWait wait=new WebDriverWait(driver, 20);
WebElement flighttab = driver.findElement(By.linkText("Flights"));
flighttab.click();
Thread.sleep(5000);
WebElement roundtrip_radio_button = driver.findElement(By.id("RoundTrip"));
roundtrip_radio_button.click();
WebElement from = driver.findElement(By.xpath(".//*[#id='FromTag']"));
WebElement to =driver.findElement(By.xpath(".//*[#id='ToTag']"));
from.clear();
from.sendKeys("Mumbai");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul/li[#class='list']")));
driver.findElement(By.xpath("//ul/li[#class='list']")).click();
to.clear();
to.sendKeys("Delhi");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul/li[#class='list']")));
driver.findElement(By.xpath("//ul/li[#class='list']")).click();
The xpath that you are using is incorrect. I am providing you the correct xpath and an alternate way to click on the auto-completer (by using className), you can use either of them, both will work fine. And as the wait.until method returns the element, you can directly perform the click on it, which would result in one atleast less operation/scraping on the page.
Autocompleter Correct Xpath:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#class='uiSelected']"))).click();
Autocompleter by className:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("uiSelected"))).click();
I want to get text/value from the tooltip which appears when hover on svg element on the graph created with highcharts.
Below is the snippet:
Tried below code:
List<WebElement> Volumelist=driver.findElements(By.xpath("//*[name()='svg']//*[name()='g'][5]//*[name()='g'][2]//*[name()='path']"));
System.out.println("Got the list!");
new Actions(driver).moveToElement(Volumelist.get(1)).clickAndHold().build().perform();
WebElement toolTip=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class='highcharts-halo highcharts-color-3'][#visibility='visible']")));
System.out.println("toolTip text= "+toolTip.getText());
First hover on the element on which tooltip appears and then execute :
String tooltipText = driver.findElement(By.cssSelector("g.highcharts-tooltip text tspan")).getAttribute("textContent");
You can ping me at sandpdangi13#gmail.com if that does not work for you.
Try to hover to tooltip element using this method if normal Actions hover is not working :
public static void mouseHoverJScript(WebDriver driver, WebElement element) {
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false);"
+ " arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
((JavascriptExecutor) driver).executeScript(mouseOverScript,
element);
I found over their examples page the values with this XPath:
driver.findElement(By.xpath(.//*[name()="g" and contains(#class,"highcharts-label")]//*[name()="tspan" and #style='font-weight:bold'])).getText();
The first step is move the mouse over one point, for example with this XPath, move to the first point:
WebElement elem = DriverUtils.driver.findElement(By.xpath(.//*[name()='path' and contains(#class, 'highcharts-point highcharts-color')][1]));
new Actions(driver).moveToElement(elem).clickAndHold().build().perform();
I used the Selenium 3.9.0, with previous 3.4.0 version doesn't work for me.
So, solution that worked for me:
WebElement elem = driver.findElement(By.xpath("//[name()='svg']//[name()='g'][5]//[name()='g'][2]//[name()='path'][1]"));
new Actions(driver).moveToElement(elem).clickAndHold().moveByOffset(1, 1).pause(1000).perform();
String text=driver.findElement(By.xpath("//[name()='svg']//[name()='g'][9]//[name()='text'][1]//[name()='tspan'][3]")).getText();
Currently, I am trying to make my progressive web apps Testing automation using selenium.As All my element I get from different shadow room.
Like
//This function will return the element inside shadow root
public WebElement GetShadowRoot(WebElement host) {
WebElement shadowRoot = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", host);
return shadowRoot;
}
WebElement MainHost = driver.findElement(By.cssSelector("vrs-
app[page='refine']"));
WebElement shadow1 = GetShadowRoot(MainHost);
WebElement host2 = shadow1.findElement(By.cssSelector("vrs-refine"));
WebElement shadow2 = GetShadowRoot(host2);
System.out.println(shadow2);
WebElement host3 = shadow2.findElement(By.cssSelector("vrs-list-
generator"));
WebElement shadow3 = GetShadowRoot(host3);
System.out.println(shadow3);
WebElement host4 = shadow3.findElement(By.cssSelector("vrs-single-property-tile:first-child"));
WebElement shadow4 = GetShadowRoot(host4);
Using This method I get my HTML element through cssSelector.
Here I am getting the issue of CSS selector vrs-single-property-tile:first-child" I have several shadow root at same name only id is different.And ID change dynamically.
Here I want to take this single propery tiles and print the property title and price
Here is my temporary URL so that you can get an idea
My goal is to access this element shadow3.findElements(By.cssSelector("#gtmTitle > h3"));
Any type of help will be appreciated.
In the following example, I have to click only for Jet Airways. It appears that the xpath selected is correct since it gives the right only from the selection.
http://i.imgur.com/8TiOgha.png
However when this same is being pulled by Selenium WebDriver, it says element not visible. But it still gives a Button with zero length text string. as given in watch window.
http://i.imgur.com/jrR0221.png
I would appreciate if anyone can help me to point if any error I am making since I am new to VBA
Not sure if your XPath is correct and is locating the right static element...
You may use this for locating 'Jet Airways'
//label[text()[contains(.,'Jet Airways')]]
Also, using .click(), try clicking on the 'Airlines' dropdown before locating the 'Airlines Dropdown' XPath
//span[#title='Airlines']
Update:
public class SkyScanner
{
static String chkBxXpth = "//label[#class='dropdown-item cfx']/input[#checked]";
public static void main(String[] args) throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
Actions actions = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.get("http://skyscanner.cntraveller.com/en-GB/flights#/result?originplace=AUH&destinationplace=LHR");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[#title='Airlines']")));
driver.findElement(By.xpath("//span[#title='Airlines']")).click();
List<WebElement> chkBx = driver.findElements(By.xpath(chkBxXpth));
for(WebElement i : chkBx)
{
actions.moveToElement(i).click().perform();
}
driver.findElement(By.xpath("//label[text()[contains(.,'Jet Airways')]]")).click();
}
}
i'm trying to upload some sonar ruleset files to multiple sonars.
i want to get help by web ui automator using Selenium.
i wrote some java code but it still doesn't work.
*added comment
bellowed code works on chrome driver but it doesn't work on safari driver.
please tell me how to modify code to work for multiple browser.
here is my code
public void openQualityProfiles() {
String linkTextSettings = "Settings";
String cssSelector = ".dropdown-menu > ul > li > a";
WebElement settings = waitForElement(By.linkText(linkTextSettings));
settings.click();
WebElement qualityProfiles = waitForElement(By.cssSelector(cssSelector));
qualityProfiles.click();
}
public WebElement waitForElement(By locator) {
WebElement target = null;
WebDriverWait wait = new WebDriverWait(driver, 10);
target = wait.until(ExpectedConditions.elementToBeClickable(locator));
return target;
}
and here is HTML
Settings
<div id="admin-panel" class="dropdown-menu" style="display: none">
<ul>
<li>Quality Profiles</li>
<li>Configuration</li>
<li>Security</li>
<li>System</li>
If your submenu appears when mouse is over, you can use:
new Actions(driver).moveToElement(yourMenu).build().perform();
or try to click on
driver.findElement(By.className("link-more")).click();