非常に奇妙な問題に直面しています。私はfacebookを開こうとしています>忘れたアカウントのリンクをクリックして>新しいタブで開きます> 2つのテキストボックスをクリックしてください。 私のコードは次のとおりです。webdriverを使用して要素をクリックできません
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class OpenLinkInNewTabTest {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "<path>\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.facebook.com");
String ParentWindowHandle = driver.getWindowHandle();
WebElement w = driver.findElement(By.linkText("Forgotten account?"));
new Actions(driver)
.keyDown(Keys.CONTROL)
.keyDown(Keys.SHIFT)
.click(w)
.keyUp(Keys.SHIFT)
.keyUp(Keys.CONTROL)
.perform();
new Actions(driver)
.sendKeys(Keys.CONTROL + "w")
.perform();
// ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
//
// driver.switchTo().window(tabs.get(1));
// WebElement fn = (new WebDriverWait(driver, 20))
// .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#identify_email")));
// System.out.println(driver.getTitle());
// fn.sendKeys("abcdejf:");
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
if(driver.getTitle().contains("Forgotten Password ")){
Thread.sleep(5000);
driver.findElement(By.cssSelector("#identify_email")).sendKeys("adf");
driver.findElement(By.name("email")).sendKeys("ASF");
driver.close();
driver.switchTo().window(ParentWindowHandle);
break;
}
}
driver.findElement(By.name("email")).sendKeys("ASF");
}
}
しかし、私は
driver.findElement(By.cssSelector("#identify_email")).sendKeys("adf");
要素に値を送信することができませんようになっています
:私のようなコードを記述する場合<input id="identify_email" class="inputtext" name="email" autofocus="1" type="text">
System.setProperty("webdriver.chrome.driver","path\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.facebook.com");
driver.findElement(By.linkText("Forgotten account?")).click();
driver.findElement(By.name("email")).sendKeys("ASF");
driver.findElement(By.cssSelector("#identify_email")).sendKeys("adf");
両方の要素を適切にクリックすることができます。 実行中にも例外が見つかりませんでした。この問題をデバッグするのを手伝ってください。おかげさまで UPDATE:
WebDriverWait wait = new WebDriverWait(driver, 10);
String email = "[email protected]";
WebElement emailInputField = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#identify_email")));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].setAttribute('value', ' " + email + "')", emailInputField);
break;
クリックすると、この要素をアクティブにします
は時々、このコードを実行するには、この要素の
Exception in thread "main" org.openqa.selenium.InvalidElementStateException: invalid element state
(Session info: chrome=56.0.2924.87)
エラーが発生していますか、フィールドが単に空白になっていますか? –
コンソールでエラーが表示されず、フィールドも空になっています。 – Avishek2585835
「電子メール」という名前で選択すると、2つの入力フィールドが生成されます。ページ上部のフォームには、そのようなフィールドがあります。 WebDriverは 'findElement(By.name(" email "))'を使って最初のものだけを返します。あなたのCSSセレクターは、しかし、動作するはずです。私はあなたが 'By.id(" identify_email ")' –