My code looks like this:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//driver.findElement(By.xpath(".//button[starts-with(#type,’submit’)]")).click();
driver.findElement(By.xpath(".//*[/html/body/div[4]/div/form/div[3]/div[2]/button']")).click();
The error is:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string './/*[/html/body/div[4]/div/form/div[3]/div[2]/button']' is not a valid XPath expression.enter image description here
Please let me know, how can I automate this?
First of all I would suggest you to go for Relative based Xpath than Absolute based.
use this below:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElementByXpath("//button[starts-with(#type,'submit')]")).click();
You have kept . before '//' which is not required and if this doesn't work lemme know.
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[2]/button")).click();
[] is for conditions. Example:
driver.findElement(By.xpath("/html/body/table/tbody/tr[contains(., 'bla')]"));
./ or .// is for finding children elements of an element. Example:
var RcdRow = driver.findElement(By.xpath("/html/body//tr"));
var RcdTd = RcdRow.findElement(By.XPath(".//td[3]"));
There is an extra quote in your code after "button". Also .//* is used for relative path so you don't have to use it here since you are using absolute path to the button element.
Related
I want to click an element by the text of a span who is the child of the element. How do I do this?
<button type="button" class="alert-button ion-focusable ion-activatable sc-ion-alert-md" tabindex="0"><span class="alert-button-inner sc-ion-alert-md">OK</span><ion-ripple-effect class="sc-ion-alert-md md hydrated" role="presentation"></ion-ripple-effect></button>
This is what I have tried but it didn't work.
#FindBy(css = "button[span:contains('OK')]")
Selenium doesn't supports the :contains pseudo-class anymore as #jari.bakken in their comment confirms:
AFAICT, the :contains pseudo-class isn't in the CSS spec and is not supported by either Firefox or Chrome (even outside WebDriver).
Further #dawagner in their comment also mentions:
contains isn't a valid CSS3 selector; because of this several browsers don't natively support contains, so Selenium (and WebDriver) don't claim to support it._
Solution
However you can still use the other attributes to construct a CssSelector to identify the element as follows:
#FindBy(css = "button.alert-button.ion-focusable.ion-activatable.sc-ion-alert-md span.alert-button-inner.sc-ion-alert-md")
As an alternative you can also use either of the following Xpath based locator strategies:
#FindBy(xpath = "//button[#class='alert-button ion-focusable ion-activatable sc-ion-alert-md']//span[#class='alert-button-inner sc-ion-alert-md']")
Using the innerText:
#FindBy(xpath = "//button[#class='alert-button ion-focusable ion-activatable sc-ion-alert-md']//span[#class='alert-button-inner sc-ion-alert-md' and text()='OK']")
or even:
#FindBy(xpath = "//span[#class='alert-button-inner sc-ion-alert-md' and text()='OK']")
even possibly:
#FindBy(xpath = "//span[text()='OK']")
CSS Selectors with Selenium do not support locating elements by their text content. Only XPath supports that.
Try this XPath:
//button//span[text()='OK']
or
//button//span[contains(.,'OK')]
or
//button//span[contains(text(),'OK')]
So the element could be defined as following
#FindBy(xpath= "//button//span[text()='OK']")
Or
#FindBy(xpath= "//button//span[contains(.,'OK')]")
the HTML looks like
<span class="linktext"><em>M</em>asters</span>
xpath - //*[#id="mastersNavButton"]/span
I tried with below codes but didn't work
driver.findElement(By.xpath("//*[#id="mastersNavButton"]/span"));
driver.findElement(By.partialLinkText("asters")).click();
First check this:
List<WebElement> emElements = driver.findElements(By.tagName("em"));
System.out.println(emElements.size());
If you get 0, you'll need to wait until the page is loaded and elements reachable by selenium and/or switch to frame.
See this https://www.guru99.com/implicit-explicit-waits-selenium.html
and this https://www.guru99.com/handling-iframes-selenium.html
Please use the following code:
driver.find_element_by_xpath("//span[contains(#class,'linktext')]//em[contains(text(),'M')]").click()
I want to find the Xpath of (plus sign) in linkedin profile in adding new education field as per screenshot
driver.findElement(By.xpath(".//*[contains(#aria-label,'Add new education')]")).click();
but an error massage found that:
Unable to locate element
Try with the below xpath.
driver.findElement(By.xpath(".//li-icon[#aria-label ='Add new education']")).click();
Note: if you are still not able to locate the element then probably you are trying to access the webelement even before it is loaded on the webpage.
In that case you need to use
//30 is the wait time in seconds.
WebDriverWait wait = new WebDriverWait(driver,30);
//This will wait for 30 seconds to locate the element before throwing an Exception.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//li-icon[#aria-label ='Add new education']")));
Let me know if this works.
Welcome to SO. Here is the locator to identify the + in education.
Using CSS a[class$='add-education ember-view']
driver.findElement(By.cssSelector("a[class$='add-education ember-view']")).click();
xpath //a[contains(#class,'add-education ember-view')]
driver.findElement(By.xpath("//a[contains(#class,'add-education ember-view')]")).click();
I am used to Selenium WebDriver were I can do something like this:
ReadOnlyCollection<IWebElement> magicPills = _webDriver.FindElements(By.CssSelector("span[id$='_Blue_Pills']"));
How do I do the same thing in WebDriverIO? I couldn't find anything in the docs that stated StartsWith, EndsWith, or whatever.
My first failed attempt is:
const magicPills = $('span.$_Blue_Pills');
Give a try as like below in wdio:
const magicPills = $$('span[id$='_Blue_Pills']');
$() returns a webElement not elements
and you can use the same cssSelector you tried in selenium_webdriver(because wdio will automatically resolves to cssSelector internally).
Please try the above and see if it works.
I am trying to enter a value into a textfield then Tab to the next field (which also enters the value). The Keys.TAB method does not seem to be working.
My code is as follows:
var Keys = JavaImporter(org.openqa.selenium.Keys)
var input = WDS.browser.findElement(pkg.By.xpath('xpath_to_input'))
input.sendKeys('value')
input.sendKeys(Keys.TAB)
I am getting the following error:
sun.org.mozilla.javascript.internal.EvaluatorException: Can't find method org.openqa.selenium.remote.RemoteWebElement.sendKeys(string). <Unknown source>
Thank you for your help. I have tried all sorts of things and it will not work.
In addition to what ekuusela suggests there are 2 more options:
Use \t escape sequence like:
input.sendKeys('value\t');
Use java.awt.Robot approach as follows:
input.sendKeys('value')
var robot = new java.awt.Robot()
var keyEvent = java.awt.event.KeyEvent
robot.keyPress(keyEvent.VK_TAB)
robot.keyRelease(keyEvent.VK_TAB)
Remember that "Robot" approach simulates native key and mouse event on the machine where it is executed so if you use remote webdriver instance it won't play.
For more WebDriver Sampler tips and tricks see The WebDriver Sampler: Your Top 10 Questions Answered guide.
If you use Java 6 you must pass the string in an array, like this:
var input = WDS.browser.findElement(pkg.By.xpath('xpath_to_input'))
input.sendKeys(['value'])
input.sendKeys([Keys.TAB])
http://jmeter-plugins.org/wiki/WebDriverSampler/