syed naveed
syed naveed

Reputation: 105

Is there a way to click an element using Partial text or complete Text using Appium

I am trying to click an element with text "No, Thanks" using below code, I tried various options so far such as

driver.findElementByXPath("//*[contains(text(),'THANKS')]").click();

 driver.findElement(By.name("No,THANKS")).click();
 driver.findElementByName("No,THANKS").click();

There is no other element with same text. I am using Appium Driver and Samsung device.

Upvotes: 0

Views: 5507

Answers (3)

Harjeet Singh
Harjeet Singh

Reputation: 1

Please try this:

driver.findElement(By.xpath("//android.widget.TextView[contains(@text,'No, Thanks')")).click();

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193298

Seems you were close. The text No, Thanks contains a , character in between which you need to avoid. So effectively you can use either of the following based Locator Strategies:

  • xpath 1:

    driver.findElementByXPath("//*[starts-with(., 'No') and contains(., 'Thanks')]").click();
    
  • xpath 2:

    driver.findElementByXPath("//*[contains(., 'No') and contains(., 'Thanks')]").click();
    

Upvotes: 1

Nithin Mohan
Nithin Mohan

Reputation: 425

If it is TextView, you can consider the following

driver.find_element_by_xpath("//android.widget.TextView[@text='No, Thanks']")

Upvotes: 2

Related Questions