通过Java Selenium在表格中输入值

huangapple go评论155阅读模式
英文:

Entering values in a table through java selenium

问题

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class TableInteractionExample {
    public static void main(String[] args) {
        // Set the path to your ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize WebDriver
        WebDriver driver = new ChromeDriver();

        // Navigate to the webpage containing the table
        driver.get("your_webpage_url_here");

        // Find all rows in the table
        List<WebElement> rows = driver.findElements(By.xpath("//table/tbody/tr"));

        // Loop through each row
        for (WebElement row : rows) {
            // Find the input element for NoOfPkgs textbox
            WebElement noOfPkgsInput = row.findElement(By.cssSelector("td:nth-child(1) input[type='text']"));

            // Send keys to the NoOfPkgs textbox
            noOfPkgsInput.sendKeys("10"); // Replace with the desired value

            // Find the checkbox for IsHazardous
            WebElement isHazardousCheckbox = row.findElement(By.cssSelector("td:nth-child(3) input[type='checkbox']"));

            // Check the IsHazardous checkbox
            isHazardousCheckbox.click(); // Replace with the desired action for checkbox interaction

            // Similar steps can be followed for other elements within the row
        }

        // Close the browser
        driver.quit();
    }
}

Make sure to replace "path/to/chromedriver" with the actual path to your ChromeDriver executable and "your_webpage_url_here" with the URL of the webpage containing the table. This example demonstrates how to interact with textboxes and checkboxes within each row of the table using Selenium WebDriver in Java. You can extend this approach to interact with other elements as needed.

英文:

Got a table with below html structure

&lt;table style=&quot;width: 100%;&quot; data-bind=&quot;foreach: CargoViewModel.PackageArray()&quot;&gt;
    &lt;tbody style=&quot;border-bottom: 1px solid black !important&quot;&gt;
        &lt;tr&gt;
            &lt;td style=&quot;width: 19%; vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                &lt;table style=&quot;width: 100%;&quot;&gt;
                    &lt;tbody&gt;
                        &lt;tr&gt;
                            &lt;td style=&quot;vertical-align: top; width: 20%; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;text&quot; style=&quot;width: 85%;&quot; data-bind=&quot;value: NoOfPkgs, hasFocus: NoOfPackagesFocus, event: { blur: OnNoOfPkgsChange }&quot; onkeypress=&quot;return allowNumericOnly(event);&quot; ondrop=&quot;AllowOnPaste(event,this,&#39;Numeric&#39;);&quot; onpaste=&quot;AllowOnPaste(event,this,&#39;Numeric&#39;);&quot; maxlength=&quot;5&quot; placeholder=&quot;Packages&quot;&gt;
                            &lt;/td&gt;
                            &lt;td colspan=&quot;2&quot; style=&quot;vertical-align: top; width: 25%; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;div class=&quot;input-group&quot; data-bind=&quot;singlePopup: { pkgtype: &#39;PkgType&#39;, pkgtypename: &#39;PkgTypeName&#39;, callback: $root.PkgTypePopup }&quot;&gt;
                                    &lt;input type=&quot;text&quot; class=&quot;igPWCTextBox&quot; data-bind=&quot;value: PkgTypeName, attr: { &#39;id&#39;: ($index() + 1) + &#39;_txtPkgName&#39; }, event: { blur: OnPkgsTypeChange.bind($data, &#39;CargoEdit&#39;) }, hasFocus: PkgTypeNameFocus&quot; oncopy=&quot;return false&quot; ondrag=&quot;return false&quot; onpaste=&quot;return false&quot; placeholder=&quot;Type&quot; id=&quot;1_txtPkgName&quot;&gt;&lt;span class=&quot;input-group-btn&quot;&gt;
                                        &lt;button class=&quot;butn&quot; type=&quot;button&quot; data-bind=&quot;attr: { &#39;id&#39;: ($index() + 1) + &#39;_btnPkgType&#39; }&quot; tabindex=&quot;-1&quot; id=&quot;1_btnPkgType&quot;&gt;&lt;i class=&quot;icon-search&quot;&gt;&lt;/i&gt;&lt;/button&gt;
                                    &lt;/span&gt;
                                &lt;/div&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td style=&quot;vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;checkbox&quot; value=&quot;0&quot; data-bind=&quot;checked: IsHazardous&quot;&gt;
                                &lt;a tabindex=&quot;-1&quot; data-bind=&quot;attr: { &#39;title&#39;: HazardousIconTitle }&quot; class=&quot;ui-icon icon-warning-sign red&quot; style=&quot;font-size: 1.4em; cursor: pointer; text-decoration: none; vertical-align: middle;&quot; href=&quot;#&quot; title=&quot;DG&quot;&gt;&lt;/a&gt;
                            &lt;/td&gt;
                            &lt;td style=&quot;vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;checkbox&quot; value=&quot;0&quot; data-bind=&quot;checked: IsReefer&quot;&gt;
                                &lt;a tabindex=&quot;-1&quot; class=&quot;ui-icon icon-asterisk blue&quot; title=&quot;Temperature Controlled&quot; style=&quot;font-size: 1.6em; cursor: pointer; text-decoration: none; vertical-align: middle;&quot; href=&quot;#&quot;&gt;&lt;/a&gt;
                            &lt;/td&gt;
                            &lt;td align=&quot;right&quot; style=&quot;vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;checkbox&quot; value=&quot;0&quot; data-bind=&quot;checked: IsNonStackable&quot;&gt;&lt;span style=&quot;margin-right: 5px; vertical-align: middle; font-weight: bold; font-size: 14px;&quot; title=&quot;Non Stackable&quot;&gt;NS&lt;/span&gt;&lt;/td&gt;
                        &lt;/tr&gt;
                    &lt;/tbody&gt;
                &lt;/table&gt;
            &lt;/td&gt;
            &lt;td style=&quot;width: 15%; vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                &lt;table style=&quot;width: 100%;&quot;&gt;
                    &lt;tbody&gt;
                        &lt;tr&gt;
                            &lt;td style=&quot;vertical-align: top; width: 25%; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;text&quot; style=&quot;width: 65%; text-align: right;&quot; data-bind=&quot;value: PkgGrossPerPiece, event: { blur: OnPkgGrossPerPiece }&quot; onkeypress=&quot;CheckDecimalNumber(event,this,IHDecimals.GrWt);&quot; ondrop=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; onpaste=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; maxlength=&quot;9&quot; placeholder=&quot;Per Piece&quot;&gt;
                                &lt;span style=&quot;margin-left: 2px;&quot; data-bind=&quot;html: CargoViewModel.DisplayWeightUOM()&quot;&gt;kg&lt;/span&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td style=&quot;vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;text&quot; style=&quot;width: 65%; text-align: right;&quot; data-bind=&quot;value: PkgGrossWeight, event: { blur: OnPkgGrossWeightChange }, hasFocus: PkgGrossWeightFocus&quot; onkeypress=&quot;CheckDecimalNumber(event,this,IHDecimals.GrWt);&quot; ondrop=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; onpaste=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; maxlength=&quot;9&quot; placeholder=&quot;Gross Weight&quot;&gt;
                                &lt;span style=&quot;margin-left: 2px;&quot; data-bind=&quot;html: CargoViewModel.DisplayWeightUOM()&quot;&gt;kg&lt;/span&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                    &lt;/tbody&gt;
                &lt;/table&gt;
            &lt;/td&gt;
            &lt;td style=&quot;width: 17%; vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                &lt;table style=&quot;width: 100%;&quot;&gt;
                    &lt;tbody&gt;
                        &lt;tr&gt;
                            &lt;td style=&quot;vertical-align: top; width: 10%; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;text&quot; style=&quot;width: 78%; text-align: right;&quot; data-bind=&quot;value: PkgLength, event: { blur: OnPkgLengthChange }&quot; onkeypress=&quot;CheckDecimalNumber(event,this,IHDecimals.IHLWH);&quot; ondrop=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; onpaste=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; maxlength=&quot;9&quot; placeholder=&quot;L&quot;&gt;
                            &lt;/td&gt;
                            &lt;td style=&quot;vertical-align: top; width: 10%; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;text&quot; style=&quot;width: 78%; text-align: right;&quot; data-bind=&quot;value: PkgWidth, event: { blur: OnPkgWidthChange }&quot; onkeypress=&quot;CheckDecimalNumber(event,this,IHDecimals.IHLWH);&quot; ondrop=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; onpaste=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; maxlength=&quot;9&quot; placeholder=&quot;W&quot;&gt;
                            &lt;/td&gt;
                            &lt;td style=&quot;vertical-align: top; width: 10%; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;text&quot; style=&quot;width: 78%; text-align: right;&quot; data-bind=&quot;value: PkgHeight, event: { blur: OnPkgHeightChange }&quot; onkeypress=&quot;CheckDecimalNumber(event,this,IHDecimals.IHLWH);&quot; ondrop=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; onpaste=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; maxlength=&quot;9&quot; placeholder=&quot;H&quot;&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td colspan=&quot;3&quot; style=&quot;vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;text&quot; style=&quot;width: 65%; text-align: right;&quot; data-bind=&quot;value: PkgVolume, event: { blur: OnPkgVolumeChange }, hasFocus: PkgVolumeFocus&quot; onkeypress=&quot;allowNumbersafterDecimalPoint(event,this,IHDecimals.VolCBM);&quot; ondrop=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; onpaste=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; maxlength=&quot;9&quot; placeholder=&quot;Volume&quot;&gt;
                                &lt;span style=&quot;margin-left: 2px;&quot; data-bind=&quot;html: CargoViewModel.DisplayVolumeUOM()&quot;&gt;m&lt;sup&gt;3&lt;/sup&gt;&lt;/span&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                    &lt;/tbody&gt;
                &lt;/table&gt;
            &lt;/td&gt;
            &lt;td style=&quot;width: 23%; vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                &lt;textarea rows=&quot;3&quot; title=&quot;Shipping Marks&quot; style=&quot;width: 4.63cm; margin-top: 3px; height: 40px; overflow-y: scroll; overflow-x: hidden; resize: none; padding: 1px;&quot; data-bind=&quot;value: MarksandNos, event: { keyup: $parent.OnKeyupCargoInput.bind($data, &#39;PkgMarks&#39;), mousedown: $parent.OnKeyupCargoInput.bind($data, &#39;PkgMarks&#39;), paste: $parent.OnKeyupCargoInput.bind($data, &#39;PkgMarks&#39;), change: OnChangeCargoInput.bind($data, &#39;PkgMarks&#39;) }&quot; placeholder=&quot;Shipping Marks&quot;&gt;&lt;/textarea&gt;
            &lt;/td&gt;
            &lt;td style=&quot;width: 27%; vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                &lt;textarea title=&quot;Description&quot; style=&quot;width: 5.51cm; margin-top: 3px; height: 40px; overflow-y: scroll; overflow-x: hidden; resize: none; padding: 1px;&quot; rows=&quot;3&quot; data-bind=&quot;value: GoodsDescription, event: { keyup: $parent.OnKeyupCargoInput.bind($data, &#39;PkgDesc&#39;), mousedown: $parent.OnKeyupCargoInput.bind($data, &#39;PkgDesc&#39;), paste: $parent.OnKeyupCargoInput.bind($data, &#39;PkgDesc&#39;), change: OnChangeCargoInput.bind($data, &#39;PkgDesc&#39;) }&quot; placeholder=&quot;Description&quot;&gt;&lt;/textarea&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;

    &lt;tbody style=&quot;border-bottom: 1px solid black !important&quot;&gt;
        &lt;tr&gt;
            &lt;td style=&quot;width: 19%; vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                &lt;table style=&quot;width: 100%;&quot;&gt;
                    &lt;tbody&gt;
                        &lt;tr&gt;
                            &lt;td style=&quot;vertical-align: top; width: 20%; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;text&quot; style=&quot;width: 85%;&quot; data-bind=&quot;value: NoOfPkgs, hasFocus: NoOfPackagesFocus, event: { blur: OnNoOfPkgsChange }&quot; onkeypress=&quot;return allowNumericOnly(event);&quot; ondrop=&quot;AllowOnPaste(event,this,&#39;Numeric&#39;);&quot; onpaste=&quot;AllowOnPaste(event,this,&#39;Numeric&#39;);&quot; maxlength=&quot;5&quot; placeholder=&quot;Packages&quot;&gt;
                            &lt;/td&gt;
                            &lt;td colspan=&quot;2&quot; style=&quot;vertical-align: top; width: 25%; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;div class=&quot;input-group&quot; data-bind=&quot;singlePopup: { pkgtype: &#39;PkgType&#39;, pkgtypename: &#39;PkgTypeName&#39;, callback: $root.PkgTypePopup }&quot;&gt;
                                    &lt;input type=&quot;text&quot; class=&quot;igPWCTextBox&quot; data-bind=&quot;value: PkgTypeName, attr: { &#39;id&#39;: ($index() + 1) + &#39;_txtPkgName&#39; }, event: { blur: OnPkgsTypeChange.bind($data, &#39;CargoEdit&#39;) }, hasFocus: PkgTypeNameFocus&quot; oncopy=&quot;return false&quot; ondrag=&quot;return false&quot; onpaste=&quot;return false&quot; placeholder=&quot;Type&quot; id=&quot;2_txtPkgName&quot;&gt;&lt;span class=&quot;input-group-btn&quot;&gt;
                                        &lt;button class=&quot;butn&quot; type=&quot;button&quot; data-bind=&quot;attr: { &#39;id&#39;: ($index() + 1) + &#39;_btnPkgType&#39; }&quot; tabindex=&quot;-1&quot; id=&quot;2_btnPkgType&quot;&gt;&lt;i class=&quot;icon-search&quot;&gt;&lt;/i&gt;&lt;/button&gt;
                                    &lt;/span&gt;
                                &lt;/div&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td style=&quot;vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;checkbox&quot; value=&quot;0&quot; data-bind=&quot;checked: IsHazardous&quot;&gt;
                                &lt;a tabindex=&quot;-1&quot; data-bind=&quot;attr: { &#39;title&#39;: HazardousIconTitle }&quot; class=&quot;ui-icon icon-warning-sign red&quot; style=&quot;font-size: 1.4em; cursor: pointer; text-decoration: none; vertical-align: middle;&quot; href=&quot;#&quot; title=&quot;DG&quot;&gt;&lt;/a&gt;
                            &lt;/td&gt;
                            &lt;td style=&quot;vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;checkbox&quot; value=&quot;0&quot; data-bind=&quot;checked: IsReefer&quot;&gt;
                                &lt;a tabindex=&quot;-1&quot; class=&quot;ui-icon icon-asterisk blue&quot; title=&quot;Temperature Controlled&quot; style=&quot;font-size: 1.6em; cursor: pointer; text-decoration: none; vertical-align: middle;&quot; href=&quot;#&quot;&gt;&lt;/a&gt;
                            &lt;/td&gt;
                            &lt;td align=&quot;right&quot; style=&quot;vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;checkbox&quot; value=&quot;0&quot; data-bind=&quot;checked: IsNonStackable&quot;&gt;&lt;span style=&quot;margin-right: 5px; vertical-align: middle; font-weight: bold; font-size: 14px;&quot; title=&quot;Non Stackable&quot;&gt;NS&lt;/span&gt;&lt;/td&gt;
                        &lt;/tr&gt;
                    &lt;/tbody&gt;
                &lt;/table&gt;
            &lt;/td&gt;
            &lt;td style=&quot;width: 15%; vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                &lt;table style=&quot;width: 100%;&quot;&gt;
                    &lt;tbody&gt;
                        &lt;tr&gt;
                            &lt;td style=&quot;vertical-align: top; width: 25%; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;text&quot; style=&quot;width: 65%; text-align: right;&quot; data-bind=&quot;value: PkgGrossPerPiece, event: { blur: OnPkgGrossPerPiece }&quot; onkeypress=&quot;CheckDecimalNumber(event,this,IHDecimals.GrWt);&quot; ondrop=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; onpaste=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; maxlength=&quot;9&quot; placeholder=&quot;Per Piece&quot;&gt;
                                &lt;span style=&quot;margin-left: 2px;&quot; data-bind=&quot;html: CargoViewModel.DisplayWeightUOM()&quot;&gt;kg&lt;/span&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td style=&quot;vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;text&quot; style=&quot;width: 65%; text-align: right;&quot; data-bind=&quot;value: PkgGrossWeight, event: { blur: OnPkgGrossWeightChange }, hasFocus: PkgGrossWeightFocus&quot; onkeypress=&quot;CheckDecimalNumber(event,this,IHDecimals.GrWt);&quot; ondrop=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; onpaste=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; maxlength=&quot;9&quot; placeholder=&quot;Gross Weight&quot;&gt;
                                &lt;span style=&quot;margin-left: 2px;&quot; data-bind=&quot;html: CargoViewModel.DisplayWeightUOM()&quot;&gt;kg&lt;/span&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                    &lt;/tbody&gt;
                &lt;/table&gt;
            &lt;/td&gt;
            &lt;td style=&quot;width: 17%; vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                &lt;table style=&quot;width: 100%;&quot;&gt;
                    &lt;tbody&gt;
                        &lt;tr&gt;
                            &lt;td style=&quot;vertical-align: top; width: 10%; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;text&quot; style=&quot;width: 78%; text-align: right;&quot; data-bind=&quot;value: PkgLength, event: { blur: OnPkgLengthChange }&quot; onkeypress=&quot;CheckDecimalNumber(event,this,IHDecimals.IHLWH);&quot; ondrop=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; onpaste=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; maxlength=&quot;9&quot; placeholder=&quot;L&quot;&gt;
                            &lt;/td&gt;
                            &lt;td style=&quot;vertical-align: top; width: 10%; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;text&quot; style=&quot;width: 78%; text-align: right;&quot; data-bind=&quot;value: PkgWidth, event: { blur: OnPkgWidthChange }&quot; onkeypress=&quot;CheckDecimalNumber(event,this,IHDecimals.IHLWH);&quot; ondrop=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; onpaste=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; maxlength=&quot;9&quot; placeholder=&quot;W&quot;&gt;
                            &lt;/td&gt;
                            &lt;td style=&quot;vertical-align: top; width: 10%; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;text&quot; style=&quot;width: 78%; text-align: right;&quot; data-bind=&quot;value: PkgHeight, event: { blur: OnPkgHeightChange }&quot; onkeypress=&quot;CheckDecimalNumber(event,this,IHDecimals.IHLWH);&quot; ondrop=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; onpaste=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; maxlength=&quot;9&quot; placeholder=&quot;H&quot;&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td colspan=&quot;3&quot; style=&quot;vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                                &lt;input type=&quot;text&quot; style=&quot;width: 65%; text-align: right;&quot; data-bind=&quot;value: PkgVolume, event: { blur: OnPkgVolumeChange }, hasFocus: PkgVolumeFocus&quot; onkeypress=&quot;allowNumbersafterDecimalPoint(event,this,IHDecimals.VolCBM);&quot; ondrop=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; onpaste=&quot;AllowOnPaste(event,this,&#39;Number&#39;);&quot; maxlength=&quot;9&quot; placeholder=&quot;Volume&quot;&gt;
                                &lt;span style=&quot;margin-left: 2px;&quot; data-bind=&quot;html: CargoViewModel.DisplayVolumeUOM()&quot;&gt;m&lt;sup&gt;3&lt;/sup&gt;&lt;/span&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                    &lt;/tbody&gt;
                &lt;/table&gt;
            &lt;/td&gt;
            &lt;td style=&quot;width: 23%; vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                &lt;textarea rows=&quot;3&quot; title=&quot;Shipping Marks&quot; style=&quot;width: 4.63cm; margin-top: 3px; height: 40px; overflow-y: scroll; overflow-x: hidden; resize: none; padding: 1px;&quot; data-bind=&quot;value: MarksandNos, event: { keyup: $parent.OnKeyupCargoInput.bind($data, &#39;PkgMarks&#39;), mousedown: $parent.OnKeyupCargoInput.bind($data, &#39;PkgMarks&#39;), paste: $parent.OnKeyupCargoInput.bind($data, &#39;PkgMarks&#39;), change: OnChangeCargoInput.bind($data, &#39;PkgMarks&#39;) }&quot; placeholder=&quot;Shipping Marks&quot;&gt;&lt;/textarea&gt;
            &lt;/td&gt;
            &lt;td style=&quot;width: 27%; vertical-align: top; color: rgb(51, 51, 51);&quot;&gt;
                &lt;textarea title=&quot;Description&quot; style=&quot;width: 5.51cm; margin-top: 3px; height: 40px; overflow-y: scroll; overflow-x: hidden; resize: none; padding: 1px;&quot; rows=&quot;3&quot; data-bind=&quot;value: GoodsDescription, event: { keyup: $parent.OnKeyupCargoInput.bind($data, &#39;PkgDesc&#39;), mousedown: $parent.OnKeyupCargoInput.bind($data, &#39;PkgDesc&#39;), paste: $parent.OnKeyupCargoInput.bind($data, &#39;PkgDesc&#39;), change: OnChangeCargoInput.bind($data, &#39;PkgDesc&#39;) }&quot; placeholder=&quot;Description&quot;&gt;&lt;/textarea&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

How can I send values (through sendkeys) into textboxes and checkboxes associated with each row. How to traverse through each row with in this table.

The rows can also be dynamically added but how can I send values to textboxes and select checkboxes with in this table structure

Trying to achieve this through Selenium Java

答案1

得分: 0

这段代码在经过对xpath的尝试后,已经可以正常工作。

List<WebElement> tbodyElements = driver.findElements(By.xpath("//*[@id=\"divTemplatePkgDetails\"]/div[3]/table/tbody"));
System.out.println(tbodyElements.size());

for (int i = 1; i <= tbodyElements.size(); i++) {
    String value = null;
    if (i % 2 == 0) {
        value = "Bag";
    } else if (i % 2 == 1) {
        value = "Pallet";
    }

    driver.findElement(By.xpath("//*[@id=\"divTemplatePkgDetails\"]/div[3]/table/tbody[" + i + "]/tr/td[1]/table/tbody/tr[1]/td[1]/input")).sendKeys("10");
    driver.findElement(By.xpath("//*[@id=\"divTemplatePkgDetails\"]/div[3]/table/tbody[" + i + "]/tr/td[2]/table/tbody/tr[2]/td/input")).sendKeys("100");
    driver.findElement(By.xpath("//*[@id=\"divTemplatePkgDetails\"]/div[3]/table/tbody[" + i + "]/tr/td[3]/table/tbody/tr[2]/td/input")).sendKeys("200");
    driver.findElement(By.xpath("//*[@id=\"divTemplatePkgDetails\"]/div[3]/table/tbody[" + i + "]/tr/td[1]/table/tbody/tr[2]/td[3]/input")).click();
    driver.findElement(By.xpath("//*[@id=\"divTemplatePkgDetails\"]/div[3]/table/tbody[" + i + "]/tr/td[4]/textarea")).sendKeys("SM");
    driver.findElement(By.xpath("//*[@id=\"divTemplatePkgDetails\"]/div[3]/table/tbody[" + i + "]/tr/td[5]/textarea")).sendKeys("Description");
    driver.findElement(By.id(i + "_txtPkgName")).sendKeys(value);
    Thread.sleep(2000);
    driver.findElement(By.xpath("(//li[@class='active']//a)[" + i + "]")).click();
}
英文:

This piece of code after hits and misses with the xpath(s) is working well.

List&lt;WebElement&gt; tbodyElements =  driver.findElements(By.xpath(&quot;//*[@id=\&quot;divTemplatePkgDetails\&quot;]/div[3]/table/tbody&quot;));
System.out.println(tbodyElements.size());
for(int i=1; i&lt;= tbodyElements.size(); i++)
{
String value=null;
if(i%2 == 0)
{
value=&quot;Bag&quot;;
}
else if(i%2==1)
{
value=&quot;Pallet&quot;;
}
driver.findElement(By.xpath(&quot;//*[@id=\&quot;divTemplatePkgDetails\&quot;]/div[3]/table/tbody[&quot;+i+&quot;]/tr/td[1]/table/tbody/tr[1]/td[1]/input&quot;)).sendKeys(&quot;10&quot;);
driver.findElement(By.xpath(&quot;//*[@id=\&quot;divTemplatePkgDetails\&quot;]/div[3]/table/tbody[&quot;+i+&quot;]/tr/td[2]/table/tbody/tr[2]/td/input&quot;)).sendKeys(&quot;100&quot;);
driver.findElement(By.xpath(&quot;//*[@id=\&quot;divTemplatePkgDetails\&quot;]/div[3]/table/tbody[&quot;+i+&quot;]/tr/td[3]/table/tbody/tr[2]/td/input&quot;)).sendKeys(&quot;200&quot;);
driver.findElement(By.xpath(&quot;//*[@id=\&quot;divTemplatePkgDetails\&quot;]/div[3]/table/tbody[&quot;+i+&quot;]/tr/td[1]/table/tbody/tr[2]/td[3]/input&quot;)).click();
driver.findElement(By.xpath(&quot;//*[@id=\&quot;divTemplatePkgDetails\&quot;]/div[3]/table/tbody[&quot;+i+&quot;]/tr/td[4]/textarea&quot;)).sendKeys(&quot;SM&quot;);
driver.findElement(By.xpath(&quot;//*[@id=\&quot;divTemplatePkgDetails\&quot;]/div[3]/table/tbody[&quot;+i+&quot;]/tr/td[5]/textarea&quot;)).sendKeys(&quot;Description&quot;);
driver.findElement(By.id(i+&quot;_txtPkgName&quot;)).sendKeys(value);			
Thread.sleep(2000);;
driver.findElement(By.xpath(&quot;(//li[@class=&#39;active&#39;]//a)[&quot;+i+&quot;]&quot;)).click();	
}

huangapple
  • 本文由 发表于 2020年7月24日 00:40:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63059127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定