很难使用JavaSelenuim自动按下linkedin中的连接按钮

n3h0vuf2  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(227)

我对javaselenuim还很陌生,我正在尝试制作一个自动程序,可以按linkedin上的“connect”按钮。
我试着在网上阅读解决方案,但没有成功。
我试着使用driver.findelement(by.id)和driver.findelement(by.class),但两者都不起作用。
谢谢!

package org.example.linkedin;
import java.util.Scanner;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class MainPageTest {

    Scanner scan = new Scanner(System.in);

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "C:\chromedriver.exe");
        login();
        Connect();
    }

    public static void login() throws InterruptedException {
        WebDriver driver = new ChromeDriver();
        Scanner scan = new Scanner(System.in);
        System.out.println("please enter userName ");
        String userName = scan.nextLine();
        System.out.println("please enter password");
        String password = scan.nextLine();
        driver.navigate().to("https://www.linkedin.com/uas/login");
        WebElement element = driver.findElement(By.id("username"));
        element.sendKeys(userName);
        WebElement element2 = driver.findElement(By.id("password"));
        element2.sendKeys(password);
        driver.findElement(By.className("mercado-button--primary")).click();
        WebElement element3 = driver.findElement(By.id("ember26"));
        element3.click();
        Thread.sleep(3000);
    }

    public static void Connect() throws InterruptedException {
      <button aria-label="Invite "" to connect" id="ember589" class="full-width artdeco-button artdeco-button--2 artdeco-button--full artdeco-button--secondary ember-view" type="button"><!---->

连接}}

bakd9h0s

bakd9h0s1#

您是否尝试使用xpath查找web元素,
使用chromedev工具在web页面上找到元素,并通过右键单击它来复制xpath。
如果元素在frame或iframe中,则需要在单击它之前切换到该frame或iframe。
在单击web元素之前,请确保您的网页上没有任何弹出窗口,因为弹出窗口将处于焦点位置,并且您将得到一个不可迭代的元素异常。

相关问题