2015-10-10 48 views

回答

1

我知道你已經標記了你自己的答案,但這不是正確的方法。 Selenium有多種方法可以進行等待,例如等待元素可點擊。這種方法是正確和更有效的方法。

driver.get("https://www.facebook.com/sachin-aryal/"); 
driver.findElement(By.name("xhpc_message_text")).sendKeys("Testing Java and Selenium"); 
WebDriverWait wait = new WebDriverWait(driver, 10); 
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button/span[.=\"Post\"]"))).click(); 
0

我注意到它的工作原理,當你第一次滾動到視圖按鈕,嘗試在後點擊之前添加:

 ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,259)",""); 
-1

嘗試下面的代碼..它必須爲你工作..

import java.util.List; 
import java.util.concurrent.TimeUnit; 
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.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 



public class Facebook_login { 

    public static void main(String[] args) throws InterruptedException { 
     String user_name = "facebook_user_name"; 
     String pwd = "facebook_password"; 
     WebDriver driver = new FirefoxDriver(); 
     driver.manage().window().maximize(); 
     driver.get("https://www.facebook.com"); 
     driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
     driver.findElement(By.name("email")).clear(); 
     driver.findElement(By.name("email")).sendKeys(user_name); 
     driver.findElement(By.name("pass")).clear(); 
     driver.findElement(By.name("pass")).sendKeys(pwd); 
     driver.findElement(By.xpath("//input[contains(@value,'Log In')]")).click(); 
     Thread.sleep(10000);     
     System.out.println("logged in successfully"); 
     WebElement notification = driver.findElement(By.xpath("//a[contains(@action,'cancel')]")); 
     if(notification.isDisplayed()) { 
      System.out.println("Notification is present"); 
      notification.click(); 
     } 
     WebElement status =driver.findElement(By.xpath("//textarea[@name='xhpc_message']")); 
     status.sendKeys("Hello"); 
     Thread.sleep(3000); 
     WebDriverWait wait = new WebDriverWait(driver,30); 
     wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Post']"))).click(); 


    } 
} 
相關問題