如何找到partialTagValues为动态的元素

时间:2017-08-22 11:30:43

标签: java eclipse selenium selenium-webdriver

我收到错误消息“已指定无效或非法选择器”。注意: - 在最后一个findElement语句中,我给了cssSelector(“// td [id * ='verificationMsg'] / p [2]),因为id是我能找到的最接近的唯一定位器来捕获数据。并且它的一部分没有改变。例如: - 我当前的id值是id =“m_7959323670053369637verificationMsg”,其中数字在定期发送的电子邮件中不断变化,但“verifyMsg”部分不会发生变化。

这是参考我之前的问题和我得到的建议

package Amazon;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class VerifyAmazonSignInPage {

	public static void main(String[] args) throws AWTException {
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver", "C://Selenium jars/chromedriver.exe");
		
		WebDriver driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		
	    driver.get("http://www.gmail.com");
	    driver.findElement(By.id("identifierId")).sendKeys("gaurav.thantry@gmail.com");
	    Robot rob = new Robot();
	    rob.keyPress(KeyEvent.VK_ENTER);
	    rob.keyRelease(KeyEvent.VK_ENTER);
	    driver.findElement(By.name("password")).sendKeys("******");
	    rob.keyPress(KeyEvent.VK_ENTER);
	    rob.keyRelease(KeyEvent.VK_ENTER);


	    driver.findElement(By.xpath("//span[text()='Amazon password assistance']")).click();
String v = driver.findElement(By.cssSelector("td[id*='verificationMsg']/p[2]")).getText();
	    
	    System.out.println(v);

	}

}

1 个答案:

答案 0 :(得分:0)

抓住它们。我知道了。 // * [含有(@ ID, 'verificationMsg')] / P [2]。 这有效。