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());
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();
This is the button of the page I need to click:
<div _ngcontent-jnk-c107="" class="btn btn-primary btn-outline" tabindex="0" ng-reflect-router-link="/opportunity/1">VIEW</div>
I can't use the text view in the locator as there are other 22 buttons in the same page with the same text.
The only difference between them is that router link, which I don't know how to call. I already tried with Link text and partial link text, with no results.
In case ng-reflect-router-link attribute value is unique for this element you can use the following XPath locator:
//div[#ng-reflect-router-link='/opportunity/1']
To click() on the element with text as VIEW with respect to the ng-reflect-router-link attribute you can use either of the following Locator Strategies:
Using cssSelector:
driver.findElement(By.cssSelector("div.btn.btn-primary.btn-outline[ng-reflect-router-link='/opportunity/1']")).click();
Using xpath:
driver.findElement(By.xpath("//div[#class='btn btn-primary btn-outline' and #ng-reflect-router-link='/opportunity/1']")).click();
However, as the element is a Angular element, to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
Using cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.btn.btn-primary.btn-outline[ng-reflect-router-link='/opportunity/1']"))).click();
Using xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='btn btn-primary btn-outline' and #ng-reflect-router-link='/opportunity/1']"))).click();
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')]")));
I'm using the line of code:
driver.findElement(By.xpath("//*[#id=\"sernum\"]")).sendKeys("XXXXXXXXXX");
All I need to do is insert a value in the field. But it isn't work...
The fragment from "dom":
To send a character sequence within the <input> field as the element is a dynamic element you need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#sernum[name=\"sernum\"]"))).sendKeys("XXXXXXXXXX");
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='sernum' and #name=\"sernum\"]"))).sendKeys("XXXXXXXXXX");
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");