【发布时间】:2015-02-09 01:14:09
【问题描述】:
我的场景:
- 创建一个 ID
- 在第 1 步中创建的搜索字段中输入 ID。
如果 id 在当前屏幕上可见/可用的 id 列表中可用(如下截图),我可以获取 id,然后进入搜索字段。但是,如果 id 在当前屏幕上不可见/不可用,则脚本无法检索它。在这种情况下,我将不得不单击右箭头按钮前进到下一个列表,在那里搜索 id,然后在搜索字段中输入。
如果在第一个屏幕中可用,我下面的代码会获取 id。我确实尝试使用 if 语句,但它不起作用。非常感谢任何帮助!
我的脚本:
/*3. Enter the Site ID of the advertiser created in the previous step.*/
//this finds the ANY <a> tag with the text supplied by the site_id
WebElement siteId = driver.findElement(By.xpath("//a[.='" + site_id + "']"));
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[.='" + site_id + "']")));
if(!(driver.findElement(By.xpath(PvtConstants.READ_SITES_IDS)).getText().contains("//a[.='" + site_id + "']"))){
//click on the Right-Arrow Button
driver.findElement(By.xpath(".//*[@id='dataTableSite_wrapper']/div[3]/div[2]/span[3]")).click();
//get a text/copy of the site_id
String getThePreviouslyCreatedsiteId = siteId.getText().trim();
//insert the text into the Advanced Filters Search Field
driver.findElement(By.id(PvtConstants.READ_SITES_ADVANCED_FILTER_SITE_SEARCH_FIELD)).sendKeys(getThePreviouslyCreatedsiteId);
用户界面截图:
【问题讨论】:
-
你必须使用 findElements 来知道 Id 是否存在,而不是使用 if 条件。请观看此视频以获取详细说明。 youtube.com/watch?v=4xILWxRC9aA。如果您能提供您遇到的错误,那就太好了。
-
gettext 将如何包含您传递的 xpath?屏幕上似乎没有任何可见的 xpath
标签: selenium selenium-webdriver