Suppose i have one document then how can i select text from that document using Selenium WebDriver.
I have entered below code:
d1.findElementByClassName("odd").click();
Thread.sleep(5000);
d1.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
WebElement el = d1.findElement(By.id("doc-content"));
Actions act = new Actions(d1);
act.clickAndHold(el).build().perform();
act.release().perform();
act.doubleClick(el).build().perform();
act.clickAndHold(el).build().perform();
WebElement el1 = d1.findElement(By.id("doc-data"));
act.moveToElement(el1, 50, 50).build().perform();
act.dragAndDropBy(el, 100, 150).build().perform();
act.release().build().perform();
If I understand correctly you want to Highlight found element, you can try to run js to change style for that element:
WebElement element = driver.FindElement(By.Id('someId'));
((JavascriptExecutor) driver).executeScript("arguments[0].style.backgroundColor='yellow';", element);
Or change font color:
((JavascriptExecutor) driver).executeScript("arguments[0].style.color='yellow';", element);
Related
driver.get("https://thesportstak.com/");
Thread.sleep(4000);
WebElement scrollStory = driver.findElement(By.xpath("//div[#id='story3']"));
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
wait.until(ExpectedConditions.visibilityOf(scrollStory));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", scrollStory);
List<WebElement> homeStories = driver.findElements(By.xpath("//div[#class=\"mainDiv \"]"));
List<WebElement> homeStoriesptag = driver.findElements(By.xpath("//div[#class=\"mainDiv \"]//p"));
for (WebElement homeStoryp : homeStoriesptag) {
System.out.println("getting home story p tag " + homeStoryp);
System.out.println("Text of home story " + homeStoryp.getText());
}
WebElement firstStory =homeStories.get(3);
firstStory.click();
Thread.sleep(3000);
WebElement shadow = driver.findElement(By
.cssSelector("div[class=\"i-amphtml-fill-content i-amphtml-story-player-shadow-root-intermediary\"]"));
// This Element is inside single shadow DOM.
SearchContext shadowroot = shadow.getShadowRoot();
Thread.sleep(1000);
WebElement iframe = shadowroot.findElement(By.cssSelector(" div:nth-child(1) > iframe:nth-child(3)"));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(iframe));
List<WebElement> please = driver.findElements(By.xpath("//div[#class=\"letterbox\"]//p"));
System.out.println(please);
WebElement nextbutton =driver.findElement(By.cssSelector("button[aria-label='Next page']"));
for(WebElement letterstory : please) {
wait.until(ExpectedConditions.attributeToBeNotEmpty(letterstory,"class"));
wait.until(ExpectedConditions.elementToBeClickable(nextbutton));
***nextbutton.click();*** ---->>>
I am trying to click the above button. Next button.click. I have tried both xpath and css selector to find the button.
Also, I can only fetch the text from the first story above, the rest elemenets are present, but no text can be fetched from that. the p tag and the texts are present in the DOM and can be seen in the first story itself. last line.
Kindly help regarding the same.
System.out.println("stories element "+ letterstory);
***System.out.println("stories inside "+letterstory.getText());***
}
I am using below code
Actions action=new Actions(driver);
action.moveToElement(facultyOfCivil).build().perform();
WebElement oceanManagement=driver.findElement(By.xpath("//a[#href='https://www.annauniv.edu/iom/home.php']"));
action.moveToElement(oceanManagement).build().perform();
oceanManagement.click();
After hovering the mouse on an element, i am not able to find xpath for sub-element as the sub-element is not being displayed in HTML
Use JavaScriptExecutor to click the element. Try this one,
WebDriverWait webDriverWait = new WebDriverWait(driver, 10);
Actions actions = new Actions(webDriver);
actions.moveToElement(facultyOfCivil).build().perform();
WebElement oceanManagement = webDriverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id="menuItemHilite32"]")));
((JavascriptExecutor) webDriver).executeScript("arguments[0].scrollIntoView();", oceanManagement);
((JavascriptExecutor) webDriver).executeScript("arguments[0].click();", oceanManagement);
I am trying to click on dropdown value to select city in from field in Make my trip http://www.makemytrip.com/. But getting Stale element reference exception. Ids are getting changed on page load.
Tried below code:
driver.findElement(By.xpath(".//*[#id='hp-widget__sfrom']")).clear();
driver.findElement(By.xpath(".//*[#id='ui-id-1']"));
driver.findElement(By.xpath(".//*[#id='hp-widget__sfrom']")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeSelected(driver.findElement(By.xpath(".//*[#class='ui-menu-item'][2]"))));
To click on a dropdown value e.g. Mumbai you can use the following solution:
Code Block:
driver.get("https://www.makemytrip.com/")
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='input_fromto checkSpecialCharacters ui-autocomplete-input' and #id='hp-widget__sfrom']"))).click();
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//li[#class='ui-menu-item'][starts-with(#id,'ui-id-')]//span[#class='autoCompleteItem__label']")));
for (WebElement element:myList)
if(element.getText().contains("Mumbai"));
element.click();
Browser Snapshot:
You can use this working code:
WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
driver.findElement(By.xpath(".//*[#id='hp-widget__sfrom']")).clear();
driver.findElement(By.xpath(".//*[#id='hp-widget__sfrom']")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#class='ui-menu-item'][2]/div/p[1]/span[1]"))).click();
I have fixed the xPath of dropdown list element. Always try to specify the exact element yo want to interact with. For example if you want to click on button, try to find <span> or <button> tag, for a link <a> tag and for input fields <input> tag.
You can try this code :
I do not see any use of xpath in this scenario. I have converted some of the xpath to either css selector or id. and have kept only one. Though I have not faced any stale element reference.
System.setProperty("webdriver.chrome.driver", "D:\\Automation\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get("https://www.makemytrip.com/");
WebElement from = wait.until(ExpectedConditions.elementToBeClickable(By.id("hp-widget__sfrom")));
from.click();
from.clear();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("ul[class*='ui-widget-content hp-widget__sfrom']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[contains(#aria-label,'Top Cities : Mumbai, India ')]"))).click();
The below code works fine for me and it is parameterized as well, it works for any input value without changing the xpath. In this example, I took mumbai as test data.
driver.get("https://www.makemytrip.com/");
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[contains(#id,'hp-widget__sfrom')]")).clear();
driver.findElement(By.xpath("//input[contains(#id,'hp-widget__sfrom')]")).click();
driver.findElement(By.xpath("//input[contains(#id,'hp-widget__sfrom')]")).sendKeys("Mumbai");
Thread.sleep(2000);
WebDriverWait wait = new WebDriverWait(driver, 30);
By option = By.xpath("//div[#class='autoCompleteItem']/p/span[contains(text(),'Mumbai')]");
wait.until(ExpectedConditions.elementToBeClickable(option));
driver.findElement(option).click();
private void next() {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.reddit.com/r/pics/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement element = driver.findElement(By
.xpath("//span[contains(.,'next')]"));
element.click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println(driver.getCurrentUrl());
is this code correct, all that happens when next is clicked is the focus scrolls down the page to the button
Your xpath selector is wrong.
Change it to:
WebElement element = driver.findElement(By
.xpath("//a[contains(text(),'next')]"));
or even better (in case one of the topic links contains the text "next") use:
WebElement element = driver.findElement(By
.xpath("//span[#class='nextprev']/a[contains(text(), 'next')]"));
This will ensure that the a element that is picked up is within the correct span at the bottom of the page and make your test less brittle.
The xpath = //span[contains(.,'next')] used in your code, locates span with contents view more : next › ,but you need to click only on next ›.i.e., you need to click on the anchor tag which contains next ›.
The below code will solve the issue.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.reddit.com/r/pics/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement element = driver.findElement(By
.linkText("next ›"));
element.click();
System.out.println(driver.getCurrentUrl());
It is always better to avoid xpath and use other locators like linkText or partialLinkText in this case.
I am running Selenium Web-driver using JAVA and facing an issue with auto-suggest input text field. When I enter a String "books" in the text field, an option would show up. Then I want to click or select the input populated on the auto suggest menu.
Below is the code:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.amazon.com/");
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("books");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("gwcswTooltip")));
List<WebElement> findElements = driver.findElements((By.id("gwcswTooltip").name("books on")));
for (WebElement webElement : findElements)
{
System.out.println(webElement.getText());
}
You need to just pick the right locator.
Add the following line
List<WebElement> findElements = driver.findElements((By.xpath("//div[#id='srch_sggst']/div")));
instead of
List<WebElement> findElements = driver.findElements((By.id("gwcswTooltip").name("books on")));