I want to find a p tag with class = "big-number". Here is the code I wrote:
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("big-number")));
System.out.println(driver.getTitle());
System.out.println(myDynamicElement);
Here is my output:
[[FirefoxDriver: firefox on MAC (fed46ad4-9ca9-9344-a57a-1d336db3927c)] -> class name: big-number]
I cannot identify the error, it is giving me an output but its makes no sense to me.
Any tips on how I can at least identify my error?
I am certainly sure the element is present, here is the HTML code:
<div id="users-online-container" style="">
<img class="big-number-icon" src="images/usersOnline.png">
<p class="big-number">228</p>
<p class="caption">Users Online</p>
</div>
<div id="users-online-loading"></div>
TimeOutException occurs because driver cannot find element in specific time. Problem in selector i think. If you sure that element always visible, and exists on the page so try next code:
//Select first paragraph in div
driver.FindElement(By.CssSelector("#users-online-container .big-number"));
//if you have several p with same classes you could access any of them using index. e.g.
driver.findElements(By.CssSelector(".big-number"))[index];
Selectors can be #users-online-container .big-number or .big-number. Both will work.
Try below code..
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("big-number")));
// It will print the text of the Element:
system.out.println(myDynamicElement.getText());
Also you try to locate the element with the help of XPATH and make sure your locator is uniquely identify the element. Also check that IsDisplayed() and IsEnabled() returns True.
In your code you are printing the WebElement that will print the Hashcode.
In order to get the text of the Element, you'll have to use getText() method.
Hope it will help!
Related
No idea how to address this input text field element with selenium / Java (openjdk 11 2018-09-25).
I tried xpath, cssSelector etc. it never works. Its always "Unable to locate element".
<slot name="input">
<input part="value" tabindex="0" aria-labelledby="vaadin-text-field-input-3">
</slot>
This did NOT work:
driver.findElement(By.xpath("//input[#aria-labelledby='vaadin-text-field-input-3']")).sendKeys("test");
Is there a solution for this?
UPDATE:
This thread solved partly my problem. The solution that worked for me can be found here link.
To send a character sequence to the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
css_selector:
new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(
By.cssSelector("div.vaadin-text-field-container div[part=input-field][id^='vaadin-text-field-input'] slot[name='input'] > input[part='value'][aria-labelledby^='vaadin-text-field-input']")
)).sendKeys("pixelhead");
xpath:
new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(
By.xpath("//div[#class='vaadin-text-field-container']//div[#part='input-field' and starts-with(#id, 'vaadin-text-field-input')]//slot[#name='input']/input[#part='value' and starts-with(#aria-labelledby, 'vaadin-text-field-input')]")
)).sendKeys("pixelhead");
Xpath:
//div[#class='vaadin-text-field-container']//descendant::input[#part='value' and starts-with(#aria-labelledby, 'vaadin-text-field-input')]
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
If this is unique //div[#class='vaadin-text-field-container']//descendant::input[#part='value' and starts-with(#aria-labelledby, 'vaadin-text-field-input')] then you need to check for the below conditions as well.
Check if it's in any iframe/frame/frameset.
Solution: switch to iframe/frame/frameset first and then interact with this web element.
Check if it's in any shadow-root.
Solution: Use driver.execute_script('return document.querySelector to have returned a web element and then operates accordingly.
Make sure that the element is rendered properly before interacting with it. Put some hardcoded delay or Explicit wait and try again.
Solution: time.sleep(5) or
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='vaadin-text-field-container']//descendant::input[#part='value' and starts-with(#aria-labelledby, 'vaadin-text-field-input')]"))).send_keys("test")
If you have redirected to a new tab/ or new windows and you have not switched to that particular new tab/new window, otherwise you will likely get NoSuchElement exception.
Solution: switch to the relevant window/tab first.
If you have switched to an iframe and the new desired element is not in the same iframe context then first switch to default content and then interact with it.
Solution: switch to default content and then switch to respective iframe.
You can start debugging from step1.
Update:
Solution specific to the problem:
Thread.sleep(2000);
WebElement inputButton = (WebElement) ((JavascriptExecutor)driver).executeScript("return document.querySelector('#TextFieldTitle').shadowRoot.querySelector('#vaadin-text-field-input-3 > slot:nth-child(2) > input')");
inputButton.sendKeys("test");
in the place of paste query selector here you will have to go to dev tools again in goog chrome by pressing F12, and then
Go to that input box
Do a right click
Select copy
Select copy JS path.
Ctrl + v into notepad to see what you've got from the dev tool.
It'd be something like this:
document.querySelector("#vaadin-text-field-input-3 > slot:nth-child(2) > input")
replace this paste query selector here with the stuff that is wrapped inside ""
Per the html provided, this works:
driver.find_element(By.XPATH, "//input[#part='value' and contains(#aria-labelledby, 'vaadin-text-field-input')]"]
x = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#part='value' and contains(#aria-labelledby, 'vaadin-text-field-input')]")))
x.send_keys('This is typed here by selenium')
print(f"the typed text in input box is: {x.get_attribute('value')}")
Output:
the typed text in input box is: This is typed here by selenium
Process finished with exit code 0
Looks like part of HTML looks like:
<div class="vaadin-text-field-container">
<label part="label" id="vaadin-text-field-label-3"></label>
<div part="input-field" id="vaadin-text-field-input-3">
<slot name="prefix"></slot>
<slot name="input">
<input part="value" tabindex="0" aria-labelledby="vaadin-text-field-input-3">
</slot>
</div>
</div>
Much better is to build your XPATH locator from some id field:
//div[#id='vaadin-text-field-input-3']//input[contains(#aria-labelledby, 'vaadin-text-field-input')]"]
Also, as mentioned earlier have to check:
if it is unique for this page
if the input element is not dynamic
If you found solved that comment you could move to the code part and use the founded locator with WebDriver:
driver.findElement(
By.xpath("//div[#id='vaadin-text-field-input-3']//input[contains(#aria-labelledby, 'vaadin-text-field-input-3')]"]"
)).sendKeys("test");
Also, keep in mind that you have to know that element is already loaded on a page. As suggested before you could use some explicit wait for your element.
I want to get text included in a span tag.
Here's HTML
<div class="position">
<h1>
<span class="select-text">Some Text</span>
</h1>
</div>
And I've tried this
wd.findElement(By.xpath("//div[#class="position"]//h1//span")).getText();
But I get this error all the time and IDK what I'm doing wrong
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class="position"]//h1//span"}
Additional problem: after getting a text from this page I need to make sure if this text is not visible on another page. If you have any idea how can I realize it I'll be grateful.
I believe you will find that your xpath selector for div with class 'position' should be
div[contains(#class, 'position')]
To print the text Some Text from the <span> you can use either of the following Locator Strategies:
Using cssSelector and getAttribute("innerHTML"):
System.out.println(wd.findElement(By.cssSelector("div.position>h1>span.select-text")).getAttribute("innerHTML"));
Using xpath and getText():
System.out.println(wd.findElement(By.xpath("//div[#class='position']/h1/span[#class='select-text']")).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 and getText():
System.out.println(new WebDriverWait(wd, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.position>h1>span.select-text"))).getText());
Using xpath and getAttribute("innerHTML"):
System.out.println(new WebDriverWait(wd, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='position']/h1/span[#class='select-text']"))).getAttribute("innerHTML"));
Try this xpath:
wd.findElement(By.xpath("//div[contains(#class='position')]/h1/span[contains(text(), 'Some Text') ]")).getText () ;
If you need to check, if this text exists in another page or not, you need to write the generic xpath, which will capture the results.
Compare your current string with another text using if loop.
I am coding a selenium webdriver test in Java. I need the test to click on a "Yes" button in chrome that does not have an id or name.
I cannot find the element to click on the button that only has "Yes" as its unique identifier. There is also a "No" button.
I have tried to find the WebElement using xpath, classname and I have tried findElements function. Nothing succeeds.
This is the HTML:
<span class="ui-btn-inner">
<span class="ui-btn-text popup-anchor-text">Yes</span>
</span>
I have tried:
WebElement yesBtn = browser.findElement(By.xpath("//div[#class='ui-btn-text popup-anchor-text']/span"));
WebElement yesBtn = browser.findElement(By.xpath("//span[.='Yes']"));
WebElement yesBtn = browser.findElement(By.xpath("//div[contains(text(), 'Yes')]"));
WebElement yesBtn = browser.findElement(By.xpath("//button[#class='ui-btn-text popup-anchor-text' and span='Yes']"));
WebElement yesBtn = browser.findElement(By.xpath("//div[#class='ui-btn-text popup-anchor-text' and span='Yes']"));
yesBtn.click();
List<WebElement> yesBtn = browser.findElements(By.className("ui-btn ui-shadow ui-btn-corner-all ui-btn-up-a"));
yesBtn.get(0).click();
Error message:
NoSuchElementException; no such element. Unable to locate element.
The correct XPath locator would be:
//span[text()='Yes']
Just in case you can go for normalize-space() function:
//span[normalize-space()='Yes']
If this doesn't help:
Make sure that the span doesn't belong to an iframe, otherwise you will have to switch to the iframe before attempting to locate the element.
driver.switchTo().frame("your-frame");
Make sure to use WebDriverWait class just in case the element is not immediately available so WebDriver would perform several find attempts with polling interval
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Yes']")));
Try out //span[#class='ui-btn-inner']/descendant::span[contains(text(), 'Yes')]. It should help out.
Found list of elements either with xpath or CSS (same results in browser console)
int alltips = driver.findElements(By.xpath("//div[#class='column medium-12']//div/ul/li")).size();
int alltips1 = driver.findElements(By.cssSelector("ul.feed-tips#Grid > li.feed-item")).size();
System.out.println(alltips);
System.out.println(alltips1);
As a result of both printing got same result (that there are 21 of 'li' containers exist on a page)
But, when put ran that in selenium webdriver, I got same result for both and it is zero.
Added screenshot from console
What did I do wrong?
Here is a part of HTML:
<div class="column medium-12">
<h1>Free Tips</h1>
<p>Here you'll always find the latest tips posted by our international community of sports betting tipsters. If you're ever in need of inspiration for a bet, this is the place to be! </p>
<div class="row">
<ul class="feed-tips" id="Grid" data-sport="" data-country="" data-
league="">
And below HTML looks like as on screenshot:
findElements does not throw an error if no elements are found, so it is possible that the elements are not found at the time of calling this method.
You can wait for the ul element to be visible before calling findElements by using a WebDriverWait like this
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(#Grid)));
This will wait up to 10 seconds before throwing a timeout. After that, call your findElements method. At this point you know that the parent ul is visible
int alltips = driver.findElements(By.xpath("//ul[#id='Grid']/li")).size();
I looked simillar questions at stackoverflow and didn't find anything that can help me.
I have following html code:
<span class="help-block ng-scope" translate="">Company</span>
How can i find this element (Java)? I tried following variants:
driver.findElement(By.cssSelector(".help-block.ng-scope"))
driver.findElement(By.xpath("//span[contains(#class, "Company")]"))
I even tried to just copy xpath from browser console in two ways:
First:
driver.findElement(By.xpath("//*[#id="ngCloudike"]/body/div[6]/div/div/div/div/form/div[1]/div[6]/span"))
Second:
driver.findElement(By.xpath("/html/body/div[6]/div/div/div/div/form/div[1]/div[6]/span"))
Also i tried to find By.ClassName, but i can't do this, because there are some spaces. So, how can i find element like this?
Try below expression:
driver.findElement(By.xpath("//span[text()='Company']"))
If you get NoSuchElementException with this code, try to add some time to wait until element present and visible:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Company']"))
If you get NoSuchElementException even with ExplicitWait, target element might be located inside an iframe. To handle element inside an iframe:
driver.switchTo().frame("iframeNameOrId");
driver.findElement(By.xpath("//span[text()='Company']"));