【问题标题】:The radio button is not getting selected with this HTML code for that. How to do that?此 HTML 代码未为此选择单选按钮。怎么做?
【发布时间】:2019-11-06 01:19:51
【问题描述】:
<input id="radio2" name="radioGroup" type="radio">
<label class="flRight" for="radio2">
  ::before
  "Senior Citizen"
  ::after
</label>
WebElement senior = driver.findElement(By.id("radio2"));
      senior.click();

现在的问题是代码无法点击所需的元素。

【问题讨论】:

  • 尝试使用不同的方式点击元素。 (Link)stackoverflow.com/questions/44912203/… 第二种方式对我总是有效的。
  • 您可能需要为您尝试自动化的页面发布完整的 HTML 或链接。您的定位器策略看起来不错,而且 WebDriverWait 也不能​​解决问题。这里最可能的问题是您的定位器不够具体,并且找不到要单击的正确元素。

标签: selenium selenium-chromedriver


【解决方案1】:

您需要诱导WebdriverWait 并点击单选按钮使用JavaScript Executor,因为webdriver 的点击和动作类都不起作用

WebDriverWait wait = new WebDriverWait(driver, 20); 
WebElement item=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@class='flRight' and @for='radio2']")));
JavascriptExecutor js= (JavascriptExecutor) driver; 
js.executeScript("arguments[0].click();", item);

【讨论】:

    【解决方案2】:

    试试这个:

    WebElement senior = driver.findElement(By.xpath(".//div[@class='radioBtn']/label[2]"));
        WebElement close = driver.findElement(By.xpath(".//div[@id='nvpush_cross']"));
        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.visibilityOf(close));
        close.click();
        senior.click();
    

    代码之前没有工作,因为有弹出窗口你需要关闭它。

    【讨论】:

    • 通过您提供的链接后,我已经更新了 xpath。
    • 仍然无法点击。
    • FAILED: f org.openqa.selenium.ElementClickInterceptedException: 元素点击被拦截:元素 在点( 492、602)。其他元素会收到点击:
    • 与"nvpush_cross"很好的收获
    • @MosheSlavin 我在我的环境中运行它,看到有弹出,可能是这个问题
    【解决方案3】:

    使用 WebDriverWait:

    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.id("radio2")));
    senior.click();
    

    看到网站后,看起来input 不是您要点击的元素,而是label...

    所以只需将senior var 更改为:

    WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='RadioButton']//label[@for='radio2']")));
    

    在元素不可点击的情况下自动执行人工操作的最佳方法是使用Actions:

    WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='RadioButton']//label[@for='radio2']")));
    Actions actions = new Actions(driver);
    actions.moveToElement(senior).click().build().perform();  
    

    (使用JavascriptExecutor 和executeScript 不会真正点击...它只是调用可以很好但不用于测试的方法...)

    作为最后的手段使用JavascriptExecutor:

    JavascriptExecutor jse= (JavascriptExecutor) driver; 
    jse.executeScript("arguments[0].click();", senior);
    

    【讨论】:

    • 不,它仍然无法识别元素
    • 它没有被点击......好吧,网站是“axisbank.com/retail/calculators/fd-calculator”,我需要选择老年人旁边的单选按钮
    • 你是怎么得到 xpath 的?
    • 我在 Chrome 中使用 inspect 按钮...ctrl + f 并输入任何 xpath,这是确保您的 xpath 的简单方法
    【解决方案4】:

    请尝试以下解决方案:

      WebDriverWait wait = new WebDriverWait(driver, 10);
      WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@class='flRight' and @for='radio2']")));
    
      Actions action=new Actions(driver);
      action.moveToElement(element).click().perform();
    

    【讨论】:

      猜你喜欢
      • 2019-08-28
      • 2020-05-02
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 2021-11-09
      相关资源
      最近更新 更多