如何在表中获取元素的xpath

时间:2016-11-23 07:35:21

标签: xml selenium xpath selenium-webdriver qa

我开始使用Selenium webdriver(新手:D)。我如何在表中获得元素的xpath?我试图在chrome中查看页面的源代码,后选元素并获取它的xpath。 Selenium webdriver告诉我他找不到这个xpath的元素。

页面链接: http://samples.gwtproject.org/samples/Showcase/Showcase.html#!CwCellList

例如,我如何将字符粘贴到textBox? 我试着使用这段代码:

UPD:

 @org.junit.Test
    public void getStarted(){
        System.setProperty("webdriver.chrome.driver", "/Users/fedor/Desktop/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("http://samples.gwtproject.org/samples/Showcase/Showcase.html#!CwCellList");

        WebElement t = driver.findElement(By.xpath(".//tr[td[contains(text(), 'First Name')]]//input"));

        t.clear();
        t.sendKeys("test1111");

    }

UPD 2:问题是在找到元素之前错过了隐式等待。这段代码为我修好了:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

1 个答案:

答案 0 :(得分:1)

例如来自给定的link

如果您想在名字 textBox中输入文字,请尝试使用以下xpath下面textBox找到适当的.//tr[td[normalize-space() = 'First Name:']]//input 文字: -

.//tr[td[contains(text(), 'First Name')]]//input

try {
            KeyingDataProvider kp = new FileSystemKeyStoreKeyingDataProvider(
                    "pkcs12", 
                    "C:\\workspace\\tests\\my\\LG.pfx",
                    new FirstCertificateSelector(),
                    new DirectPasswordProvider("mykeypass"),
                    new DirectPasswordProvider("mykeypass"), 
                    true);
            XadesSigningProfile p = new XadesBesSigningProfile(kp);
            XadesSigner signer = p.newSigner();

            javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            javax.xml.parsers.DocumentBuilder builder = null;
            builder = factory.newDocumentBuilder();


            Document doc1 = builder.parse(new File("C:\\workspace\\tests\\document.xml"));
            Document doc2 = builder.parse(new File("C:\\workspace\\tests\\content.xml"));

            Node contentElement = doc2.getDocumentElement();
            Node parentElement = doc1.getDocumentElement();
            Node adoptedContentElement = doc1.adoptNode(contentElement);
            parentElement.appendChild(adoptedContentElement);

            Node nodeToSign = doc1.getDocumentElement().getFirstChild();
            Node nodeToAttachSignature = doc1.getDocumentElement();

            IndividualDataObjsTimeStampProperty dataObjsTimeStamp = new IndividualDataObjsTimeStampProperty();
            AllDataObjsCommitmentTypeProperty globalCommitment = AllDataObjsCommitmentTypeProperty.proofOfApproval();
            CommitmentTypeProperty commitment = CommitmentTypeProperty.proofOfCreation();   

            DataObjectDesc obj = new EnvelopedXmlObject(nodeToSign, "text/xml", null);
            SignedDataObjects dataObjs = new SignedDataObjects(obj).withCommitmentType(globalCommitment); 

            signer.sign(dataObjs, nodeToAttachSignature);

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            Result output = new StreamResult(new File("C:\\workspace\\tests\\signedDocument.xml"));
            Source input = new DOMSource(doc1);
            transformer.transform(input, output);


        } catch (KeyStoreException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (XadesProfileResolutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerFactoryConfigurationError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XAdES4jException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }