2017-03-26 23 views
0

enter image description here selenium webdriverを使用してインライン要素をクリックすることはできません。ここでselenium webdriverを使用してインライン要素をクリックできません

は、正方形のアイコンがあるイメージリンク(右側上部)のほかにURL https://www.google.com/.

です。そのアイコンをクリックしてMapsを選択する必要があります。 スクリーンショットが添付されています。 xpath、cssselector、ID、Nameを使用しましたが、何も動作していません。 誰も私にこれを手伝ってもらえますか?

コード:

import java.util.List; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.Select; 

public class webelements2 { 
    public static void main(String[] args) throws InterruptedException 
    { 
     System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe"); 
     WebDriver driver = new FirefoxDriver(); 
     driver.get("https://www.google.com/"); 
     driver.manage().window().maximize(); 
     driver.findElement(By.id("gbwa")).click(); 
     driver.findElement(By.className("gb_3")).click();    
    }  
} 

答えて

0

あなたが要素をクリックしたとき、それは製品ページが開きますので、このコードは、何らかの形でFirefoxで動作していない - By.id("gbwa")。しかし、Chromeで同じことを試してもうまくいきます。変更する必要があるのはBy.className("gb_3")By.xpath("//ul[@class='gb_ka gb_da']/li[3]")です。

WebDriver driver = new ChromeDriver(); 
driver.get("https://www.google.com/"); 
driver.manage().window().maximize(); 
driver.findElement(By.id("gbwa")).click(); 
driver.findElement(By.xpath("//ul[@class='gb_ka gb_da']/li[3]")).click(); 

Firefoxの場合、製品ページを開いたときに、そこから[マップ]オプションをクリックできるようにコードを変更できます。

+0

こんにちはアニッシュ、返信いただきありがとうございます。もう一度ページが商品ページに移動します。マップアイコンをクリックしていません。 –

0

コントロールにはいくつかの動的待機を追加する必要があります。さらに、Googleがあなたのホームページとして設定されていない場合、「ここに頻繁に来てください」というメッセージが表示されます。 Googleをあなたのホームページにしてください。 'このメッセージは処理する必要があります。それ以外の場合は、アプリアイコンへのアクセスに失敗します。

import java.util.List; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.Select; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 
public class webelements2 { 
    public static void main(String[] args) throws InterruptedException 
    { 
     System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe"); 
     WebDriver driver = new FirefoxDriver(); 
     driver.get("https://www.google.com/"); 
     driver.manage().window().maximize(); 
     WebDriverWait wait = new WebDriverWait(driver, 10); 
     if (driver.findElements(By.xpath("//a[@title='No thanks']")).size() !=0) { 
      wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[@title='No thanks']")))); 
      driver.findElement(By.xpath("//a[@title='No thanks']")).click(); 
     } 

     driver.findElement(By.xpath("//div[@id='gb']//div[@id='gbwa']/div/a[@title='Google apps' and @role='button']")).click(); 

     wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[@id='gb8']/span[text()='Maps']")))); 
     driver.findElement(By.xpath("//a[@id='gb8']/span[text()='Maps']")).click();    
    }  
} 

このコードを試して、さらに質問があれば教えてください。

+0

ありがとうMahi。 GoogleはFirefoxブラウザでデフォルトに設定されていました。私がurコードを使用したとき、それはWAITを初期化するように要求しています。 –

+0

**待機**はコードで初期化されています。必要なインポートステートメントを追加しましたか? – Mahipal

+0

コードは私のために働いていた。すべての依存関係が解決されたことを確認する必要があるかもしれません。 – Mahipal

0

私は以下のコードをテストして動作します。

driver.get("https://www.google.com"); 
driver.findElement(By.cssSelector("a[title='Google apps']")).click(); 
new WebDriverWait(driver, 3).until(ExpectedConditions.elementToBeClickable(By.id("gb8"))).click(); 

基本的には、アプリアイコンをクリックし、地図アイコンをクリック可能にしてからクリックする必要があります。

私はあなたがGoogleのために働いていないと仮定しています。この場合、UIをテストする理由は何ですか? https://maps.google.comに直接移動して、UIのクリックをスキップできます。

+0

こんにちはジェフ、返事をありがとう。もう一度ページが商品ページに移動します。マップアイコンをクリックしていません。 –

+0

あなたの質問では、「画像以外のリンク(右上の上部)に四角いアイコンがあります。アイコンをクリックして地図を選択する必要があります。地図アイコンをクリックすることを意味しない場合、それはどういう意味ですか? – JeffC

0

これはどうですか...私のために働いた。

driver.get("https://www.google.com/"); 

    WebDriverWait wait = new WebDriverWait(driver, 10); 
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='gbwa']//a[@title='Google apps']")))); 
    driver.findElement(By.xpath(".//*[@id='gbwa']//a[@title='Google apps']")).click(); 

    //click map icon 
    driver.findElement(By.cssselector("a#gb8")).click(); 
0

私のためにこれを使用してください。

Firefoxドライバの代わりにChromeドライバを使用してください。

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 

public class GoogleMaps { 
    public static void main(String[] args) throws InterruptedException { 
     System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe"); 
     WebDriver driver = new ChromeDriver(); 
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
     driver.get("https://www.google.com/"); 
     driver.manage().window().maximize(); 
     driver.findElement(By.xpath("//*[@id='gbwa']")).click(); 
     driver.findElement(By.xpath("//li/a[@id='gb8']")).click(); 
     Thread.sleep(6000); 
     driver.quit(); 
    } 
} 

出力:

enter image description here

関連する問題