2013-05-16 14 views
2

나는 학생입니다. Selenium Webdriver를 사용하여 Gmail의 로그 아웃 기능을 자동화하려하지만 그렇게 할 수 없습니다.Selenium Webdriver를 사용하여 Gmail에서 링크를 클릭 할 수 없습니다.

로그 아웃에 두 가지 단계가 있으며 맨 위에 오른쪽 링크를 클릭 한 다음 로그 아웃을 클릭하십시오. 나는 그렇게 할 수 없다. 여기

<span id="gbi4t" style="max-width: 76px; text-align: left;">Mahmood Ali</span> 

<a id="gb_71" class="gbqfbb" href="?logout&hl=en&hlor" onclick="gbar.logger.il(9,{l:'o'})" role="button" target="_top">Sign out</a> 

내가

거기
driver.findElement(By.id("gbi4t")).click(); OR 

driver.findElement(By.xpath("//*[@id='gbi4t']")).click(); 

driver.findElement(By.id("gb_71")).click(); OR 

driver.findElement(By.xpath("//*[@id='gb_71']")).click(); 

몇 가지 아이디어 등의 시도

//*[@id="gbi4t"] -> Clicking that top to get the logout pop up 

//*[@id="gb_71"] -> To logout the gmail application 

내 XPath는 무엇입니까?

+0

수 없습니다. – Learner

+0

user1177636 맞습니다. Gmail에서 초급자로 일하지 마십시오.이 작업을 수행 할 수있는 오픈 소스 웹 응용 프로그램이 있습니다. 내 대답은 여기를 참조하십시오. - http://stackoverflow.com/questions/15785962/wrong-button-is-clicked- 15787796 # 15787796 – user2087450

+0

에 오류가 발생했습니다. org.openqa.selenium.NoSuchElementException : 요소를 찾을 수 없습니다 : { "method": "id", "selector": "gbi4t" } 명령 지속 시간 또는 시간 초과 : 3.03 초 – Learner

답변

0

사실 <span>은 요소로 인식되지 않습니다.

<a id="gbg4" ...> ~ click()을 사용해야하며 팝업이 표시 될 때까지 기다렸다가 <a id="gb_71" class="gbqfbb" ...>을 클릭하면 로그 아웃됩니다. 당신이 pracctice 할 필요가 있기 때문에

나는 당신에게 코드를 보자 : P

가 무슨 일이야 말해.

제안 :

내가 당신에게 제안 할 수 무엇이 cssSelector()을 사용하는 것입니다.

왜? 그것은 xpath보다 빠르며 google이나 다른 사람들이 id/name에 사용되는 동적 값을 사용할 때 클래스 속성을 사용하는 것이 더 좋으며 cssSelector()은 다른 것보다 훨씬 낫습니다.

하지만 가끔은 당신은 내부 텍스트로 "취소"한 요소 (exemple : <a>cancel</a>) 찾기 위해 XPath를 사용합니다 다음 또한 시도 할 수

cssSelector() reference

0

을 :

driver.find_element(:id, "gbgs4dn").click 
driver.find_element(:id, "gb_71").click 

이것은 나를 위해 일했다.

+0

고마워요 4 답장 안녕하세요, 저를 위해서 일하는 것도 괜찮습니까? gbgs4dn이 어디에 있는지 말해주십시오. [ Learner

+0

위의 사례에서 내 계정에는 '알림'& 'Google 공유'버튼이 누락 된 행에 '노래 부름'팝업이 표시되지 않습니다. 귀하의 경우 '알림'및 'Google 공유'버튼이 있습니다. 시도하십시오 : driver.find_element (: id, "gbi4t"). & driver.find_element (: id, "gb_71")를 클릭하십시오. – TDHM

+0

위의 솔루션이 효과가 없었습니까? – TDHM

0

이 코드는 확실히 나를 위해 작동 :

// (after logging to google.com) 
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds); 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("gbi4t"))); 
    //open overlay 
driver.findElement(By.id("gbi4t")).click(); 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("gb_71"))); 
    //press logout 
driver.findElement(By.id("gb_71")).click(); 
0

Heres는 해결 된 예 : 요소를 식별 할 수

package testme; 

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


public class testexample { 

    public static WebDriver driver; 
    public static WebElement element; 

    public static void main(String args[]) throws InterruptedException { 
     //setting the chrome driver 
     System.setProperty("webdriver.chrome.driver", "C:/Users/workspace/Downloads/chromedriver.exe"); 
     driver = new ChromeDriver(); 


     driver.get("http://www.gmail.com"); 

     element =driver.findElement(By.linkText("Sign in")); 
     element.click(); 
     Thread.sleep(1000); 
     element = driver.findElement(By.id("Email")); 
     element.sendKeys("[email protected]"); 
     element = driver.findElement(By.id("Passwd")); 
     element.sendKeys("yourpassword"); 
     element.submit(); 

     Thread.sleep(1000); 
     //click on the logout link step 1 
     element = driver.findElement(By.xpath("//*[@id='gb']/div[1]/div[1]/div/div[3]/div[1]/a")); 
     element.click(); 
     // click on actual logout button step 2 
     element = driver.findElement(By.id("gb_71")); 
     element.click(); 
     //closing the webdriver window after successful completion of the test 
     driver.close(); 
     } 
    } 
관련 문제