If the text exists then click on xyz
else click on abc
.
I'm using the below if
statement:
if(driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div/div/table/tbody/tr[6]/td[2]")).isDisplayed()) { driver.findElement(By.linkText("logout")).getAttribute("href"); } else { driver.findElement(By.xpath("/html/body/div/div/div/a[2]")).click(); }
Script fails with the following error message:
Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div/div/div/div/div/div/table/tbody/tr[6]/td[2]"}
New Selenium IDE There are more than one ways to find it. We can use the getPageSource() method to fetch the full page source and then verify if the text exists there. This method returns content in the form of string. We can also check if some text exists with the help of findElements method with xpath locator.
We can verify if an element is present using Selenium. This can be determined with the help of findElements() method. It returns the list of elements matching the locator we passed as an argument to that method. In case there is no matching element, an empty list [having size = 0] will be returned.
text() and contains methods text(): A built-in method in Selenium WebDriver that is used with XPath locator to locate an element based on its exact text value. contains(): Similar to the text() method, contains() is another built-in method used to locate an element based on partial text match.
Try this code:
The below code used to check the text presence in the entire web page.
if(driver.getPageSource().contains("your Text")) { //Click xyz } else { //Click abc }
If you want to check the text on a particular web element
if(driver.findElement(By.id("Locator ID")).getText().equalsIgnoreCase("Yor Text")) { //Click xyz } else { //Click abc }
Here we can use try ,except function using python web driver. see below the code
webtable=driver.find_element_by_xpath("xpath value") print webtable.text try: xyz=driver.find_element_by_xpath(("xpath value") xyz.click() except: abc=driver.find_element_by_xpath(("xpath value") abc.click()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With