0

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.

Guy
  • 46,488
  • 10
  • 44
  • 88
syed naveed
  • 85
  • 2
  • 12
  • Could show me your website? – jizhihaoSAMA Mar 05 '20 at 01:59
  • Have you tried to inspect your element with the Appium inspector? Based on my experience, text on iOS mobile elements might be available from `name` or `label` attributes. And it is better to use iOSNsPredicate strategy instead of xpath. It works way faster! – Yevhen Danchenko Mar 05 '20 at 16:11

3 Answers3

2

If it is TextView, you can consider the following

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

Nithin Mohan
  • 372
  • 1
  • 9
1

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();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Please try this:

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