比较浏览器中两个选项卡的数据硒



我在第一页上有价格的项目,点击";查看交易"按钮,打开第二页的新选项卡,在第二页上我有关于这个的所有信息和价格。我的主要目标是比较这些价格,但我对此有意见。

here方法,在这个方法上我有一个异常。没有这样的元素,似乎没有将驱动程序转移到这个选项卡或页面,如果添加driver.switchTo((.window(tabs.get(1((;那么我有空指针异常

`public int parsePriceOnProductPage(){
// driver.switchTo().window(tabs.get(1));
wait.until(ExpectedConditions.elementToBeClickable(bookNowBtn));
String offerPriceValue = offerPriceProductPage.toString();
String onlyNumbers = offerPriceValue.replaceAll("[^\d]", "");
int offerPrice_val = Integer.parseInt(onlyNumbers);
return offerPrice_val;
}`

这是以前在测试中从第一页开始的方法,在这里我将驱动程序发送到第二页

`public ProductPage clickOnDeal(){
viewDealBtn.get(1).click();
return new ProductPage(driver,wait);
}`

在这里我比较一下的价格

public boolean comparePriceOnHomePageWithProductPage(){
if(homePage.parsePriceFromSelectedDealOnHomePage() == productPage.parsePriceOnProductPage()){
return true;
}else if (homePage.parsePriceFromSelectedDealOnHomePage() > productPage.parsePriceOnProductPage()){
System.out.println("The price on the Home Page more than on the Product Page on the $"+ (homePage.parsePriceFromSelectedDealOnHomePage() - productPage.parsePriceOnProductPage()) );
} else {
System.out.println("The price on the Home Page less than on the Product Page on the $"+ (productPage.parsePriceOnProductPage() - homePage.parsePriceFromSelectedDealOnHomePage()) );
} return false;
}

您必须获得窗口句柄,它将打开选项卡的句柄,您可以使用以下代码在选项卡之间切换

ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
driver.switchTo().window(tabs2.get(0));
driver.close();

我在机器中尝试过的确切场景。请在我的代码下面找到。你是否使用了下面的,但仍然不起作用?

public class testclass {
public static void main(String[] args) throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.amazon.co.uk/");
Thread.sleep(3000);
WebElement element = driver.findElement(By.xpath("//span[.='Fire TV Stick Lite']"));
Actions act = new Actions(driver);
act.keyDown(Keys.CONTROL).click(element).keyUp(Keys.CONTROL).build().perform();
// driver.get("http://google.com");
Thread.sleep(3000);
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
System.out.println(driver.getTitle());
secondtab t = new secondtab(driver);
t.searchGoogle();
}
}

package boots.uk.dps.pages.base;
import com.boots.thor.base.ExtendedWebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.FindBy;
import java.util.List;
public class secondtab {
private WebDriver driver;
public secondtab(WebDriver driver){
this.driver =  driver;
}
public void searchGoogle(){
driver.findElement(By.xpath("//*[@id='buy-now-button']")).click();
}
}

我看不出您的代码中有任何错误。我认为您使用的是页面对象模型。在您的场景中,尝试使用以下方法,它应该可以工作。

driver.findElement(By…(;

最新更新