Selenium 如何处理每次动态变化的XPath?

Selenium 如何处理每次动态变化的XPath?,selenium,xpath,selenium-webdriver,selenium-ide,browser-automation,Selenium,Xpath,Selenium Webdriver,Selenium Ide,Browser Automation,我正在编写selenium web驱动程序脚本。脚本的场景是: 1) 登录www.yahoomail.com 2) 成功登录 3) 单击“撰写”按钮 4) 在“收件人”字段中输入电子邮件ID 5) 在“主题”字段中输入主题。 6) 在“文本”字段中输入电子邮件正文。 7) 单击“发送”按钮。 使用Firepath,我获取了“撰写”页面中提到的字段的xpath。 但每次打开撰写页面时,XPath都会动态变化 以下是我正在使用的脚本: WebDriver oYahoo = new Firef

我正在编写selenium web驱动程序脚本。脚本的场景是:
1) 登录www.yahoomail.com
2) 成功登录
3) 单击“撰写”按钮
4) 在“收件人”字段中输入电子邮件ID
5) 在“主题”字段中输入主题。
6) 在“文本”字段中输入电子邮件正文。
7) 单击“发送”按钮。

使用Firepath,我获取了“撰写”页面中提到的字段的xpath。 但每次打开撰写页面时,XPath都会动态变化

以下是我正在使用的脚本:

    WebDriver oYahoo = new FirefoxDriver();
    oYahoo.get("http://www.yahoomail.com/");
    oYahoo.manage().window().maximize();
    oYahoo.findElement(By.xpath(".//*[@id='login-username']")).sendKeys("abcdefasdf@yahoo.com");
    oYahoo.findElement(By.xpath(".//*[@id='login-passwd']")).sendKeys("sfgas234@123");
    oYahoo.findElement(By.xpath(".//*[@id='login-signin']")).click();
    oYahoo.findElement(By.xpath(".//*[@id='Compose']/button")).click();
    oYahoo.findElement(By.xpath(".//*[@id='yui_3_16_0_1_1448364357109_2222']")).sendKeys("abcdefgh@gmail.com");
    oYahoo.findElement(By.xpath(".//*[@id='subject-field']")).sendKeys("Hi This is my first automated mail");
    oYahoo.findElement(By.xpath(".//*[@id='yui_3_16_0_1_1448364357109_1966']")).sendKeys("Hi This is my first automated mail");
    oYahoo.findElement(By.xpath(".//*[@id='yui_3_16_0_1_1448364357109_2465']")).click();
    oYahoo.quit();
我们能做些什么,以便动态地使用Xpath?我们需要在某个类中映射这些内容吗?我们必须为“撰写”弹出窗口编写任何其他类吗?
如果“是”,那么我们怎么做呢?

试着观察一个每次都重复的常见模式。id前缀或类前缀,任何东西

然后在
By.xpath(..)
函数中使用以下选择器


/*[contains(@id,'your-common-pattern')]

我在一个项目中工作,在这个项目中,每次生成页面时,类名都是随机的,并且我找到了获取这些标签/input/的唯一方法。我是否使用了
nth-child()
使用css选择器执行函数。思考方法如下:我希望从第一个表的第二行输入第三个输入

看看这把小提琴,明白我的意思。

这是自动化的基本问题。我建议您查找id为“yui_*”的“first”元素,或者查找一个可以找到其子元素的基元素。