У меня проблемы с взаимодействием со страницей входа в Instagram с использованием Java Selenium. Я пытался использовать Actions
так:
WebDriver driver = new SafariDriver();
driver.get("https://www.instagram.com/accounts/login/");
WebElement login_elem = driver.findElement(By.name("username"));
WebElement pw_elem = driver.findElement(By.name("password"));
Actions action = new Actions(driver);
action.moveToElement(login_elem).click();//Also tried this with a 'moveByOffset()'
action.sendKeys(username);
action.moveToElement(pw_elem).click();
action.sendKeys(password);
action.perform();//nothing happens
WebElement element = driver.findElement(By.className("_t38eb _ov9ai")).findElement(By.tagName("button"));//element isn't visible
element.click();//Element isn't visible
и я также пытался использовать ExpectedConditions
:
public void login( ) {
driver = new SafariDriver();
driver.get("https://www.instagram.com/accounts/login/");
//Element isn't visible: waitElement times out. I've checked all the parts of the xml string, and it works until the '/input' at the end
waitElement("//*[@id='react-root']/section/main/div/article/div/div[1]/div/form/div[1]/div/div[1]/input[@class = '_ph6vk _jdqpn _o716c']",20);
waitElement("//*[@id='react-root']/section/main/div/article/div/div[1]/div/form/div[2]/div/div[1]/input",20);
driver.findElement(By.xpath("//*[@id='react-root']/section/main/div/article/div/div[1]/div/form/div[1]/div/div[1]/input")).sendKeys(username);
driver.findElement(By.xpath("//*[@id='react-root']/section/main/div/article/div/div[1]/div/form/div[2]/div/div[1]/input")).sendKeys(password);
driver.findElement(By.xpath("//*[@id='react-root']/section/main/div/article/div/div[1]/div/form/div[1]/div/div[1]/input")).sendKeys(Keys.RETURN);
Rest(3,true);
driver.get("https://www.instagram.com/" + username);
}
public void waitElement(String element, int time){
WebDriverWait wait = new WebDriverWait(driver, time);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(element)));
}
public void Rest(int seconds, boolean msg){
try {
if(msg)
Thread.sleep(seconds*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
и он определенно не работает с обычным findElement()
-> sendKeys()
:
WebDriver driver = new SafariDriver();
driver.get("https://www.instagram.com/accounts/login/");
Thread.sleep(5000);//Wait for page to load
WebElement input = driver.findElement(By.className("_ph6vk _jdqpn _o716c"));//find the text box
input.sendKeys(username);//ElementNotVisibleException is thrown here.
Как вводить строки в поля формы?
РЕДАКТИРОВАТЬ: также пробовал следующее:
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class Test {
public static void main(String... args) {
WebDriver driver = new SafariDriver();
driver.get("https://www.instagram.com/accounts/login/");
/*line 13*/ new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name='username']"))).sendKeys("AlbertLiu");
}
}
Вот ошибка, которую я получил:
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.cssSelector: input[name='username'] (tried for 20 second(s) with 500 milliseconds interval)
Capabilities {applicationCacheEnabled: true, browserName: safari, cleanSession: true, cssSelectorsEnabled: true, databaseEnabled: true, handlesAlerts: true, javascriptEnabled: true, locationContextEnabled: false, nativeEvents: true, platform: MAC, platformName: MAC, rotatable: false, version: 13604.5.6, webStorageEnabled: true}
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:81)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:271)
at selenium.Test.main(Test.java:13)
Используя By.xpath("//input[@name='username']")
, я получил эту ошибку:
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //input[@name='username']] (tried for 20 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:81)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:271)
at selenium.Test.main(Test.java:13)