首页 > 解决方案 > 无法与 Instagram 登录字段交互

问题描述

我在使用 Java Selenium与Instagram 登录页面交互时遇到问题。我试过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)

标签: javareactjsseleniumselenium-webdriverinstagram

解决方案


要将文本发送到带有电话号码、用户名或电子邮件<input>的帮助文本的字段,您可以删除并诱导WebDriverWaitExpectedConditions的结合使用为elementToBeClickable,如下所示:Thread.sleep(5000)

driver.get("https://www.instagram.com/accounts/login/");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name='username']")))sendKeys("AlbertLiu");
//or
driver.get("https://www.instagram.com/accounts/login/");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='username']]")))sendKeys("AlbertLiu");

推荐阅读