WebElement NumberofRecord = driver.findElement(By.xpath("//*[#id=\"tableContent\"]/div[2]/span/a"));
NumberofRecord.getText();
System.out.println(NumberofRecord);
trying to get the text but returning URL
Result: xpath: //*[#id="tableContent"]/div[2]/span/a]
Expected Output: All 18418 Messages in Table are selected. Clear selection(this need to be displayed)
Induce WebDriverWait and wait for visibilityOfElementLocated() and then get the text value.
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='tableContent']/div[2]/span/a")));
WebElement NumberofRecord = driver.findElement(By.xpath("//*[#id='tableContent']/div[2]/span/a"));
System.out.println(NumberofRecord.getText());
Related
I want to get a text which is inside a div. I tried to find help from other questions, but most of them say .getText() which doesnt exist anymore.
This is the inspect:
Im using a chrome extension with which i can copy out the XPath, but it doesnt reads the text inside the div. It copies me this here:
//div[#class='mud-alert-message']
To print the text Die Datei... you can use either of the following Locator Strategies:
Using cssSelector and getAttribute("innerHTML"):
System.out.println(wd.findElement(By.cssSelector("div.mud-alert-message")).getAttribute("innerHTML"));
Using xpath and getText():
System.out.println(wd.findElement(By.xpath("//div[#class='mud-alert-message']")).getText());
Ideally, to extract the text you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using cssSelector and getText():
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.mud-alert-message"))).getText());
Using xpath and getAttribute("innerHTML"):
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='mud-alert-message']"))).getAttribute("innerHTML"));
I believe is something like this:
WebElement element = new WebDriverWait(driver, ...).until(ExpectedConditions.elementToBeXXX(By.LOCATOR_TYPE));
String text = element.getText();
Im trying to use selenium to select the first result in a search.
To click on the first image im using xpath way to find the first result from the search.
The code is
driver.findElement(By.xpath("//*[#id='category-search-wrapper']/div[2]/div/ol/li[1]/article/a")).click();
and from using the f12 and then ctrl f tools on the website it shows that I have the correct xpath
However, it is not clicking on the Image.
Here is the website that I am trying to test if it's any help.
https://www.dunnesstores.com/search?keywords=lamp
To click() on the first result from the search you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
driver.get("https://www.dunnesstores.com/search?keywords=lamp")
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#onetrust-accept-btn-handler"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("ol.product-list.product-list-main > li a"))).click();
xpath:
driver.get("https://www.dunnesstores.com/search?keywords=lamp")
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#id='onetrust-accept-btn-handler']"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ol[#class='product-list product-list-main']/li//a"))).click();
Browser Snapshot:
driver = webdriver.Chrome()
driver.get('https://www.dunnesstores.com/search?keywords=lamp')
accept = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,
"#onetrust-accept-btn-handler")))
accept.click()
driver.find_element_by_css_selector(
'[class="c-product-card__link"]').click()
You have to first click accept cookies , then find element using class or xpath or css
Try:
element = driver.findElement(By.xpath("//*[#id='category-search-wrapper']/div[2]/div/ol/li[1]/article/a"))
driver.executeScript("arguments[0].click();", element)
With this code you avoid the elements that obscures it and the elements that are not disponible in your actual screen (out of scroll).
I am trying to inspect an element in Iframe
I am able to succesfully get into iframe but when trying to inspect element -Exception comes element not intertracble
Below is the html part and the search button I am trying to click
I tried xpath and inspection like following
//driver.findElement(By.className("gsc-search-button-v2")).click();
//driver.findElement(By.cssSelector("button.gsc-search-button")).click();
//driver.findElement(By.xpath("//button[text()='gsc-search-button']")).click();
//driver.findElement(By.cssSelector("button[class='gsc-search-button gsc-search-button-v2']")).click();
//driver.findElement(By.cssSelector(".gsc-search-button.gsc-search-button-v2")).click();
//driver.findElement(By.xpath("//button[contains(#class, 'gsc-search-button gsc-search-button-v2")).click();
//wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".gsc-search-button.gsc-search-button-v2"))).sendKeys(Keys.ENTER);
Also by giving waits also like
WebDriverWait wait = new WebDriverWait(driver, 30);
//wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".gsc-search-button.gsc-search-button-v2"))).sendKeys(Keys.ENTER);
html code
To click() on the element to search, as the the desired element is within a <iframe> so you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use either of the following Locator Strategies:
Using cssSelector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe_cssSelector")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("td.gsc-search-button > button.gsc-search-button.gsc-search-button-v2"))).click();
Using xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("iframe_xpath")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[#class='gsc-search-button']/button[#class='gsc-search-button gsc-search-button-v2']"))).click();
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
How to click within the username field within ProtonMail signup page using Selenium and Java
I am trying to select the value for Subjects field in the following form: https://demoqa.com/automation-practice-form
It is an input field that dynamically gives suggestions based on our input and later we need to select values from those suggestions. I am unable to select the desired value.
The Below code only populates the input area but the value is not selected.
driver.findElement(By.id("subjectsInput")).sendKeys("English");
driver.findElement(By.id("subjectsInput")).click(); //This line doesnot click on the desired value.
How to Select the desired value. Please help.
The code below worked for me.
WebDriver Driver = new ChromeDriver();
Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//Driver.manage().window().maximize();
String url = "https://demoqa.com/automation-practice-form";
Driver.get(url);
WebElement products=Driver.findElement(By.id("subjectsInput"));
products.sendKeys("English");
products.sendKeys(Keys.ARROW_DOWN);
products.sendKeys(Keys.ENTER);
To invoke click on the only auto suggestion English you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
driver.get("https://demoqa.com/automation-practice-form");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#subjectsInput"))).sendKeys("English");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.subjects-auto-complete__menu"))).click();
xpath:
driver.get("https://demoqa.com/automation-practice-form");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='subjectsInput']"))).sendKeys("English");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(#class, 'subjects-auto-complete__menu')]"))).click();
Browser Snapshot:
I have the following HTML code where I need to detect the number of records displayed on the page and select the correct record. However, inspecting the elements with Firefox show the records are having the same class name while the id of the WebElement is dynamically created.
If I use it to locate by tag name, it will return more than 100 Web elements.
May I know how do I identify use XPath or CSS selector to obtain the first record displayed on the page.
enter image description here
WebElement menuList = driver.findElement(By.cssSelector(".scrollable-content"));
List<WebElement> search_li = menuList.findElements(By.tagName("div"));
WebElement searchresult = driver.findElement(By.ByXPath.className("grid-view-drop-area ng-star-inserted"));
searchresult.click();
Thank you DebanjanB and Guy for your valueable input.
I used the combination of your input to get identify the unique UI object.
List<WebElement> searchresult = driver.findElements(By.xpath("//div[#class='scrollable-content']/div[#class='grid-view-drop-area ng-star-inserted' and starts-with(#id, 'grid-row')]"));
searchresult.get(0).click();
A bit of more information interms of the HTML of the expanded <div> tags would have helped us to construct a canonical answer. However, to extract the number of records displayed on the page you have to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use either of the following Locator Strategies:
cssSelector:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.grid-view-drop-area.ng-star-inserted[id^='grid-row']"))).size());
xpath:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[#class='grid-view-drop-area ng-star-inserted' and starts-with(#id, 'grid-row')]"))).size());
To obtain the first record you can use either of the following solution:
cssSelector:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibility_of_element_located(By.cssSelector("div.scrollable-content>div.grid-view-drop-area.ng-star-inserted[id^='grid-row']")));
xpath:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibility_of_element_located(By.xpath("//div[#class='scrollable-content']/div[#class='grid-view-drop-area ng-star-inserted' and starts-with(#id, 'grid-row')]")));