【问题标题】:Cannot manage to click on a button using Xpath无法设法使用 Xpath 单击按钮
【发布时间】:2020-11-22 18:42:01
【问题描述】:

您好,我正在尝试在 chrome 浏览器上使用 Xpath 单击按钮,但由于某种原因,该软件没有单击它。 我使用 devtools 检查将 Xpath 复制到 findElement 函数。 这是网站:https://mynames.co.il/ 对不起,那是希伯来语...

this photo shows the button ,i marked the button in blue

这是步骤文件:

package stepDefinitions;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import cucumber.api.PendingException;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class purchaseDomainSteps {

    
    
WebDriver driver;
    
    
    @Before
    public void setup() throws IOException {
        System.setProperty("webdriver.chrome.driver", Paths.get(System.getProperty("user.dir")).toRealPath() +  "\\src\\test\\java\\drivers\\chromedriver.exe");
        this.driver = new ChromeDriver();
        this.driver.manage().window().maximize();
        this.driver.manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);
    }
    
    @After() 
    public void tearDown() {
        this.driver.manage().deleteAllCookies();
        this.driver.quit();
    }
    
    
    
    

@Given("^I access https://mynames\\.co\\.il$")
public void i_access_https_mynames_co_il() throws Throwable {
    driver.get("https://mynames.co.il/");
    throw new PendingException();
}

@When("^I click on Login button\\.$")
public void i_click_on_Login_button() throws Throwable {
    
    
    String path = "/html/body/div[1]/div/div/section[2]/div/div/div[2]/div/div/section/div/div/div[2]/div/div/div/div/div/a/span/span";
    //WebDriverWait wait = new WebDriverWait(driver, 5);
    driver.findElement(By.xpath(path)).click();
    throw new PendingException();
}

这是跑步者类:

package runners;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)

@CucumberOptions(features = { "src/test/java/featurefiles/" }, glue = {
        "stepDefinitions" }, monochrome = true, tags = {}, 
                plugin = { "pretty", "html:target/cucumber", "json:target/cucumber.json",
                "com.cucumber.listener.ExtentCucumberFormatter:output/report.html" })


public class MainRunner {

    
}

【问题讨论】:

    标签: java selenium xpath css-selectors webdriverwait


    【解决方案1】:

    由于元素是动态元素,所以要在元素上click(),您需要将WebDriverWait 诱导为elementToBeClickable(),您可以使用以下任一Locator Strategies

    • cssSelector:

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.elementor-button-link.elementor-button.elementor-size-xs span.elementor-button-text"))).click();
      
    • xpath:

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='elementor-button-text' and text()='כניסה']"))).click();
      

    【讨论】:

      【解决方案2】:

      你有任何异常吗?如果是,那是什么例外?如果不是,则硒单击正在发生,但应用程序未确认单击。所以尝试使用javascript执行器执行如下点击

      WebElement ele = driver.findelement(By.xpath("//span[text()='כניסה']"));
      JavascriptExecutor js = (JavascriptExecutor)driver;
      js.executeScript("argument[0].click();",ele); 
      

      即使使用了上面的代码,如果它不起作用,那么检查你是否在junit runner class cucumberOptions 'glue'和'features'中引用了正确的特性和类文件

      一个更重要的提示:我在你的脚本中看到绝对 xpath,如果将来 DOM 结构发生变化,这将不起作用,所以总是选择相对 xpath

      【讨论】:

        【解决方案3】:

        在我查看网站 (https://mynames.co.il/) 后:

        <div class="elementor-button-wrapper">
           <a href="https://dash.mynames.co.il/login" target="_blank" class="elementor-button-link elementor-button elementor-size-xs" role="button">
              <span class="elementor-button-content-wrapper">
                 <span class="elementor-button-text">כניסה</span>
              </span>
           </a>
        </div>
        

        我推荐2个选项:

        1. 使用链接本身在登录页面重定向
        String targetPage = driver.findElement(By.xpath("/html/body/div[1]/div/div/section[2]/div/div/div[2]/div/div/section/div/div/div[2]/div/div/div/div/div/a")).getAttribute("href");
        driver.navigate().to(targetPage);
        
        1. (也许这就是你想要的)你点击 href 或 force a href 像按钮一样操作
        driver.findElement(By.xpath("/html/body/div[1]/div/div/section[2]/div/div/div[2]/div/div/section/div/div/div[2]/div/div/div/div/div/a")).click();
        

        您的代码不起作用的原因是因为您假定为按钮的span 没有任何点击动作,而您希望的点击动作在a href 上。

        // this is just text with style inside span
        <span class="elementor-button-text">כניסה</span>
        

        【讨论】:

          【解决方案4】:

          你好,请使用这个 Xpath 来点击你用蓝色线标记的那个按钮

          //span[contains(text(),'כניסה')]
          

          【讨论】:

          • 用于存储 Web 元素的对象存储库文件在哪里?
          • 你尝试过这个 xpath 吗?如果你不介意你能在这里分享你的项目结构你遇到了什么错误,请添加 altho screenshot。
          猜你喜欢
          • 1970-01-01
          • 2016-11-26
          • 2020-04-26
          • 1970-01-01
          • 2016-02-21
          • 2021-08-30
          • 2019-05-30
          • 2013-07-21
          • 1970-01-01
          相关资源
          最近更新 更多