Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

java - How do I locate an element whose partialTagValues are dynamic

I get an error saying 'An invalid or illegal selector was specified'. Note:- in the last findElement statement, I have given the cssSelector as ("//td[id*='verificationMsg']/p[2]) because the id is the closest unique locator I could find to capture the data. And that part of it doesn't change. eg:- My current id value is id="m_7959323670053369637verificationMsg" where the number keeps changing in the recurring emails but "verificationMsg" part of it doesn't.

this is in reference to my previous question and the suggestions that I got

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);

	}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Scratch it guys. I got it. //*[contains(@id,'verificationMsg')]/p[2]. This works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...