How do I find an element with xpath? - java

I'm trying to find a "button" and click it but it has no id.
I tried using Xpath and even cssSelector but nothing works.
This are the elements of the button:
div style="cursor: grabbing;" title="Approved Forms" onclick="goToForms(0)" class="col-lg-2 col-sm-6"
I tried this :
driver.findElement(By.xpath("//div[contains(#title=,'Approved Forms')]")).click();
driver.findElement(By.xpath("/html/body/div[3]/div/div/div[2]/div[1]")).click(); *copy xpath*
driver.findElement(By.cssSelector(['title="Approved Forms"'])).click(); *has a sintax errors, not sure how to write it*

To locate the element you can use either of the following Locator Strategies:
cssSelector:
WebElement element = driver.findElement(By.cssSelector("div[title='Approved Forms'][onclick^='goToForms']"));
xpath:
WebElement element = driver.findElement(By.xpath("//div[#title='Approved Forms' and starts-with(#onclick, 'goToForms')]"));
Ideally, you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
cssSelector:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[title='Approved Forms'][onclick^='goToForms']")));
xpath:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#title='Approved Forms' and starts-with(#onclick, 'goToForms')]")));

Related

How to get value between ::before & ::after?

I would like to get value from text between ::before ::after in selenium. I suppose it's impossible to do it by xpath, what JSexecutor code I can use?
Value I would like to get:
::before and ::after are the css and are not printable text. So to print the text Widget you can use either of the following Locator Strategies:
Using cssSelector:
System.out.println(wd.findElement(By.cssSelector("div.linkbox.margin-bottom-20 > h1.heading")).getText());
Using xpath:
System.out.println(wd.findElement(By.xpath("//div[#class='linkbox margin-bottom-20']/h1[#class='heading']")).getText());
Ideally, to extract the text Some Text as the element is a dynamic element, you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using cssSelector:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.linkbox.margin-bottom-20 > h1.heading"))).getText());
Using xpath:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='linkbox margin-bottom-20']/h1[#class='heading']"))).getText());

Not able to find an element in iframe using Xpath and Selenium

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

How to extract the text from the sibling text nodes of the strong tag using Selenium and Java

There are 52 page objects as follows, all of which has Strong tag containing a String called "Gideon".
How possibly can I get all of them by findElements method?
The "quotes" are within a text nodes. So to retrieve the text you have to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:
xpath:
List<WebElement> parentElements = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//strong[text()='Gideon']//..")));
for (WebElement parentElement:parentElements)
System.out.println(((JavascriptExecutor)driver).executeScript("return arguments[0].lastChild.textContent;", parentElement).toString());
xpath with preceding:
List<WebElement> parentElements = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//strong[text()='Gideon']//preceding::p[1]")));
for (WebElement parentElement:parentElements)
System.out.println(((JavascriptExecutor)driver).executeScript("return arguments[0].lastChild.textContent;", parentElement).toString());

Unable to find gif image by Xpath/classname/cssSelector using selenium webdriver java

I am trying to locate image using Selenium webdriver but unable to locate it by Xpath/cssSelector
I have tried cssSelector and xpath but not working.
<img alt="" class="i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" src="https://tpc.googlesyndication.com/simgad/303052068860032968">
By cssSelector -->
WebElement elementOut = driver.findElement(By.cssSelector(".i-amphtml-fill-content.i-amphtml-replaced-content"));
By Xpath -->
WebElement elementOut = driver.findElement(By.xpath("//*[#id='aw0']/amp-img/img"));
I need to locate the image.
Snapshot of the page source:
Your image resides within an iframe
So you will need to execute driver.switchTo() function prior to attempting to locate element which is inside the iframe.
Once done you should be able to use the XPath expression like:
driver.findElement(By.xpath("//img[contains(#class,'replaced-content')]"));
To click() on the image, as the the desired element is within an <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:
cssSelector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[id$='com/Web/News24/Homepage_20'][name^='google_ads_iframe_'][title='3rd party ad content']")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("img.i-amphtml-fill-content.i-amphtml-replaced-content[src^='https://tpc.googlesyndication.com/simgad']"))).click()
xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(#id, 'com/Web/News24/Homepage_20') and starts-with(#name, 'google_ads_iframe_')][#title='3rd party ad content']")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//img[#class='i-amphtml-fill-content i-amphtml-replaced-content' and starts-with(#src, 'https://tpc.googlesyndication.com/simgad')]"))).click();

How to handle the following dynamic ID through Selenium and Java?

Would you mind helping me to understand how I can handle this dynamic ID?
Here are cases which I have already tried:
driver.findElement(By.xpath("//input[contains(#id,'Username')]")).sendKeys("aaa");
driver.findElement(By.xpath("//input[starts-with(#id,'undefined-undefined-Username-')]")).sendKeys("aaa");
driver.findElement(By.xpath("//*[#type='text']")).sendKeys("aaa");
No way to find that element.
As per the HTML you have shared the element is a dynamic element . To invoke click() on the desired element you can use either of the following solutions:
cssSelector:
driver.findElement(By.cssSelector("label[for^='undefined-undefined-Username-']")).sendKeys("aaa");
xpath:
driver.findElement(By.xpath("//label[starts-with(#for,'undefined-undefined-Username-')][contains(.,'Username')]")).sendKeys("aaa");
Update
As the element is dynamic you may need to induce WebDriverWait for the desired element to be clickable as follows:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[for^='undefined-undefined-Username-']"))).sendKeys("aaa");
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[starts-with(#for,'undefined-undefined-Username-')][contains(.,'Username')]"))).sendKeys("aaa");

Categories