0

I don't want to extract by Xpath, I would like a clearer and cleaner code. thank u my World Brothers

From Xpath, OK!!

Assert.assertEquals("Digite um e-mail ou número de telefone",driver.findElement(By.xpath("//*[@id=\"view_container\"]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[2]/div[2]/div")).getText());

enter image description here

Tadeu
  • 67
  • 6

1 Answers1

1

Assert.assertEquals("Digite um e-mail ou número de telefone",driver.findElement(By.xpath(//span[contains(text(),'Digite um e-mail ou')]).getText()));

user895565
  • 101
  • 5
  • Do not Work: Assert.assertEquals("Digite um e-mail ou número de telefone",driver.findElement(By.xpath("//span[contains(text(),'Digite um e-mail ou')]").getText())); The message erro on the gettext is: The method get text is undefined for the type by – Tadeu Apr 25 '20 at 19:57
  • your question was about writing clean xpath: //span[contains(text(),'Digite um e-mail ou')] – user895565 Apr 25 '20 at 20:14
  • Yes!!... Can you helpe- Me? – Tadeu Apr 25 '20 at 20:21
  • getText() is of WebElement method so you need to do something like this `WebElement element = driver.findElement(By.XPATH(//span[contains(text(),'Digite um e-mail ou')]).getText()); Assert.assertEquals("Digite um e-mail ou número de telefone",element.toString())` – user895565 Apr 25 '20 at 20:47
  • Let it go, nothing you put is working !! .. It would be interesting for you to try to do it on your machine from the image that I put, but as it is not possible we stay here!. – Tadeu Apr 25 '20 at 20:49
  • This drug only runs via absolute xpath !! ..Assert.assertEquals("Digite um e-mail ou número de telefone",driver.findElement(By.xpath("//*[@id=\"view_container\"]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[2]/div[2]/div")).getText()); Thread.sleep(2000); System.out.println(driver.findElement(By.xpath("//*[@id=\"view_container\"]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[2]/div[2]/div")).getText()); driver.close(); } – Tadeu Apr 25 '20 at 21:09
  • 1
    This is a working sample code (exactly what i posted above)... I have just tested this.. .. Can't help you more`// declaration and instantiation of objects/variables System.setProperty("webdriver.gecko.driver","geckodriver.exe");WebDriver driver = new FirefoxDriver();String baseUrl = "http://www.walmart.com";String expectedTitle = "Welcome: Mercury Tours";WebElement actualTitle;driver.get(baseUrl); // get the actual value of the text actualTitle = driver.findElement(By.xpath("//span[contains(text(),'COVID')]")); String txt= actualTitle.getText().toString(); driver.close()` – user895565 Apr 25 '20 at 21:14
  • Thank U my brother!! I got it here, look this!!! Assert.assertEquals("Digite um e-mail ou número de telefone",driver.findElement(By.cssSelector("div[jsname='B34EJ']")).getText()); – Tadeu Apr 25 '20 at 21:22
  • Little annoying thing!! – Tadeu Apr 25 '20 at 21:24