简体   繁体   English

硒-单击由javascript生成的单选按钮

[英]selenium - clicking on radio button generated by javascript

My code is written in Java. 我的代码是用Java编写的。

I have a set of radio button that is dynamically generated by javascript. 我有一组由javascript动态生成的单选按钮。 I want to click on one of them. 我要单击其中之一。

I use xpath but it doesn't work. 我使用xpath,但是不起作用。 It says 它说

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: 线程“主要” org.openqa.selenium.NoSuchElementException中的异常:无法找到元素:

I tried both deliveryType method below, after I "Inspect elements" on google chrome. 在谷歌浏览器上“检查元素”后,我尝试了下面的两个deliveryType方法。

 //WebElement deliveryType = driver.findElement(By.xpath("//div[contains(@id, 'checkout-shipping-method-load')]//*[contains(@name, 'shipping_method')]"));
    //WebElement deliveryType = driver.findElement(By.cssSelector("[name='shipping_method'][id='s_method_matrixrate_matrixrate_5983']"));
    //deliveryType.click();

Below is the inspected element for that radio button. 以下是该单选按钮的检查元素。

 <div id="checkout-shipping-method-load">
  <dl class="sp-methods">
  <dt>Select Shipping Method</dt>
    <dd>
        <ul>
          <li>
            <input name="shipping_method" type="radio" value="matrixrate_matrixrate_5983" id="s_method_matrixrate_matrixrate_5983" class="radio validation-passed">
<label for="s_method_matrixrate_matrixrate_5983">Standard Shipping<span class="price">NT$300</span></label></li>
         <li>
           <input name="shipping_method" type="radio" value="matrixrate_matrixrate_5991" id="s_method_matrixrate_matrixrate_5991" class="radio validation-passed">
<label for="s_method_matrixrate_matrixrate_5991">Express Shipping<span class="price">NT$1,200</span></label></li>
        </ul>
    </dd>
  </dl>
</div>

I have tried to code my script to click on it. 我试图编码我的脚本以单击它。 I tried my xpath to follow "inspect element" and "view page source". 我试图让我的xpath遵循“检查元素”和“查看页面源代码”。 Both give me same error as no such element found. 两者都给我相同的错误,因为找不到这样的元素。

How do I make my selenium script to click on one of the radio button that are dynamically generated by javascript? 如何使我的Selenium脚本单击由javascript动态生成的单选按钮之一?

My xpath should point to raw javascript coding or the html generated by javascript after window load? 我的xpath应该指向原始javascript编码还是窗口加载后由javascript生成的html?

Kindly help me. 请帮助我。

Closed: I have used Implict Wait to wait for my javascript to run and load my html. 已关闭:我使用了隐式等待来等待我的JavaScript运行并加载我的html。 After loaded, I used List to store all of my radio buttons. 加载后,我使用列表存储了所有单选按钮。 From the List, I get it by index. 从列表中,按索引获取。

Thanks to Girish Sortur & JeffC. 感谢Girish Sortur和JeffC。

Below is my code. 下面是我的代码。

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
List<WebElement> methods = driver.findElements(By.cssSelector("input[name='shipping_method']"));
methods.get(0).click();

The best way to click on a dynamic element is to wait for it to appear. 单击动态元素的最佳方法是等待它出现。 In Selenium you can wait for an element until it appears using WebDriverWait() method. 在Selenium中,您可以使用WebDriverWait()方法等待元素出现,直到它出现。 ExpectedConditions are used in Selenium to check for expectations. Selenium中使用ExpectedConditions来检查期望值。 Here you can use expect the visibility of the element. 在这里您可以使用期望元素的可见性。 Here's a sample code - 这是一个示例代码-

WebElement radioBtn = driver.findElement(By.xpath("//input[@id='s_method_matrixrate_matrixrate_5983']"));
new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOf(radioBtn));

Also you can try with FluentWait which is the best option to wait for an element to appear. 您也可以尝试使用FluentWait ,这是等待元素出现的最佳选择。 Here's how to do it - 这样做的方法-

new FluentWait<WebElement>(radioBtn).
    withTimeout(50, TimeUnit.SECONDS).
    pollingEvery(2,TimeUnit.SECONDS);

FluentWait waits for an element to appear with the options that we specify. FluentWait等待元素与我们指定的选项一起显示。 Polling checks for the element's presence at that interval. 轮询以该时间间隔检查元素的存在。 Provide timeout based on the time element takes to appear in DOM. 根据出现在DOM中的时间元素提供超时。 Hope this helps. 希望这可以帮助。

I would pull all the INPUT s corresponding to shipping methods into a collection methods and then click the one that I wanted. 我会将与运送方法相对应的所有INPUT都拉到一个收集methods ,然后单击我想要的一个。 Sample code is below. 示例代码如下。

List<WebElement> methods = driver.findElements(By.cssSelector("input[name='shipping_method']"));
methods.get(0).click(); // clicks the first element, Standard Shipping

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM