I would like to know how I can validate if an element exists in order to click it, I need to get a boolean value to know it. If the button exists I need to click it if not I need to skip the click() process.
I’m using appium with typescript
Thank you!
wreed
May 4, 2023, 6:42pm
2
Use a try/catch in your code:
https://stackoverflow.com/questions/54649465/how-to-do-try-catch-and-finally-statements-in-typescript
So write a method to find the element. If element is found return true. If you catch a “No Element Found” exception, then return false.
Here is a tutorial on writing Appium tests in Typescript:
Using TypeScript in conjunction with WebdriverIO, we can develop automation tests for our Android and iOS apps that replicate the same user…
Reading time: 14 min read
1 Like
siva1
May 25, 2023, 12:38pm
3
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class ElementValidation {
private AppiumDriver driver;
public ElementValidation(AppiumDriver<WebElement> driver) {
this.driver = driver;
}
public boolean isElementPresent(By locator) {
return !driver.findElements(locator).isEmpty();
}
public void clickIfElementExists(By locator) {
if (isElementPresent(locator)) {
driver.findElement(locator).click();
}
}
}
1 Like
wreed
May 25, 2023, 2:58pm
4
@siva1 can you do it in typescript?
Giuma
May 25, 2023, 3:39pm
5
Mayra_Tabares:
I would like to know how I can validate if an element exists in order to click it, I need to get a boolean value to know it. If the button exists I need to click it if not I need to skip the click() process.
I’m using appium with typescript
you can use the isElementDisplayed function to check if an element exists and is displayed on screen.
something like this :
try {
const button = await driver.findElement(By.id(‘your-button-id’));
const isDisplayed = await button.isDisplayed();
if (isDisplayed) {
await button.click();
console.log('Button clicked!');
} else {
console.log('Button does not exist or is not displayed.');
}
} catch (error) {
console.log(‘Error occurred while validating and clicking the button:’, error);
}
give a look to this javascript - Selenium WebDriver wait till element is displayed - Stack Overflow
1 Like
maybe will help as idea in JS
// Java
@iOSXCUITFindBy(id = "supportBarButton")
@AndroidFindBy(id = "iv_toolbar_menu")
private WebElement supportButton;
@Step("Check 'SupportButton' loaded {sec}")
public boolean isSupportButtonLoaded(int sec) {
Logger.log("sec: '" + sec + "'");
setLookTiming(sec);
return super.isLoaded(supportButton);
}
// where
public void setLookTiming(int sec) {
PageFactory.initElements(new AppiumFieldDecorator(driver, Duration.ofSeconds(sec)), this);
}
public boolean isLoaded(WebElement el) {
boolean bool = false;
try {
bool = !((RemoteWebElement) el).getId().isEmpty(); // no matter enabled or not
} catch (Exception e) {
}
setDefaultTiming();
return bool;
}
siva1
May 26, 2023, 3:42am
7
Yes it is possible in typescript too.,
function findElement(array: any[], element: any): boolean {
try {
const index = array.indexOf(element);
if (index !== -1) {
return true;
} else {
throw new Error(“No Element Found”);
}
} catch (error) {
if (error.message === “No Element Found”) {
return false;
} else {
throw error;
}
}
}
2 Likes
Hey guys, thanks a lot for all the help. I will try with the answers you all gave me <3