新网页上的页面元素,具有旧页面的重定向

时间:2017-07-07 07:36:31

标签: java selenium-webdriver

我在网页上:

https://www.alibaba.com/products/MAc_book.html

点击以下第一个产品:

element3 = (new WebDriverWait(driver, 10)).until(ExpectedConditions
                .visibilityOfElementLocated(By
                        .xpath("//a[@data-hislog='60639615540']")));
        element3 = driver.findElement(By.xpath("//a[@data-hislog='60639615540']"));
        element3.click();

在新网页上,访问“联系供应商”按钮但没有运气

element1 = (new WebDriverWait(driver, 20000)).until(ExpectedConditions
                .presenceOfElementLocated(By
                        .xpath("//a[@title='Click to send an inquiry']")));
        element1 = driver.findElement(By.xpath("//a[@title='Click to send an inquiry']"));

也使用了以下代码smipets,但仍然无法

>driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); 
>driver.wait(4000);
>Thread.sleep(20000);

2 个答案:

答案 0 :(得分:0)

试试下面的代码

WebDriver driver = new FirefoxDriver();
//Open Url
driver.get("https://www.alibaba.com/products/MAc_book.html");
Thread.sleep(3000);
//Gets all listed items in the page
List<WebElement> ele = driver.findElements(By.xpath(".//div[@class='l-page']//div[@class='l-page-main']//div[@class='m-product-item']//div[1]/a/img"));
//First link of that page
WebElement element3 = ele.get(0);
element3.click();
Set<String> windows=driver.getWindowHandles();
Iterator itr=(Iterator) windows.iterator();
//Moves to the newly opened window
while(itr.hasNext())
{
    driver.switchTo().window((String)itr.next());
    System.out.println("Window title is"+driver.getTitle());
}
//Click on Contact Supplier
element3 = driver.findElement(By.xpath(".//a[@title='Click to send an inquiry']"));
element3.click();

答案 1 :(得分:0)

试试这段代码:

我已经尝试了它并且它正在运行:

    driver.get("https://www.alibaba.com/products/MAc_book.html");

    new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@data-hislog='60639615540']")));

    WebElement element3 = driver.findElement(By.xpath("//a[@data-hislog='60639615540']"));

    element3.click();

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

    new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[normalize-space(text())='Contact Supplier']")));

    WebElement contactSupplier = driver.findElement(By.xpath("//a[normalize-space(text())='Contact Supplier']"));

    System.out.println(contactSupplier.getText());`