【问题标题】:How to fix a click which is not working specific to a website如何修复不适用于特定网站的点击
【发布时间】:2023-04-04 21:47:01
【问题描述】:

登录的HTML代码如下:

<input id="login:login_btn" name="login:login_btn" onclick="A4J.AJAX.Submit('login',event,{'oncomplete':function(request,event,data){},'similarityGroupingId':'login:login_btn','parameters':{'login:login_btn':'login:login_btn'} } );return false;" value="Login" style="width:100%;margin-left:7px;" type="button">

我正在尝试简单地单击登录按钮。我的代码在这里:

System.setProperty("webdriver.chrome.driver","C:\\Users\\SmitaThakur\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://myportal-qa.pricechopper.com/mobile/eas/login.seam");
driver.findElement(By.id("login:username")).sendKeys("757823");
driver.findElement(By.id("login:password")).sendKeys("texas5");
driver.findElement(By.id("login:login_btn")).click();

我也尝试过其他点击。 此代码单击但没有任何反应。不会出现下一页,也不会出现任何错误。我已经在其他网站上尝试过相同的代码,并且效果很好。同一网站的相同代码在同一台笔记本电脑上运行良好,但以不同的人身份登录。

【问题讨论】:

  • 这可能是您正在自动化的网站的错误...
  • 嗨@smita,在点击Thread.sleep(3)之前尝试延迟一些时间
  • 在点击线上下断点,在debug中运行测试。确保页面已完全加载,现在再次启动脚本。它现在有效吗?如果是这样,则可能是时间问题。尝试找到页面上要加载的最后一个内容并等待它然后尝试点击。

标签: selenium selenium-webdriver xpath css-selectors webdriverwait


【解决方案1】:

该元素是AJAX 元素,因此请确定您需要为elementToBeClickable 诱导WebDriverwait 的元素,您可以使用以下任一解决方案:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[id$='login_btn'][name$='login_btn'][value='Login']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(@id, 'login_btn') and contains(@name, 'login_btn')][@value='Login']"))).click();
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    相关资源
    最近更新 更多