无法用selenium java单击按钮

时间:2017-09-22 08:12:14

标签: java xpath selenium-chromedriver

在此之前我使用以下代码取得了一些成功。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" /> <xsl:strip-space elements="*"/> <xsl:variable name="space" select="' '" /> <xsl:variable name="comma" select="','" /> <!-- identity transform --> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()" /> </xsl:copy> </xsl:template> <xsl:template match="Item"> <xsl:copy> <Code><xsl:value-of select="Code" /></Code> <CustomerID><xsl:value-of select="Customer/@id" /></CustomerID> <FullName> <xsl:value-of select="concat(Customer/Title, $space, Customer/FirstName, $space, Customer/MiddleName, $space, Customer/LastName)" /> </FullName> <Address> <xsl:value-of select="concat(Customer/Address/HouseNumber, $comma, Customer/Address/Street, $comma, Customer/Address/Country)" /> </Address> <ContactNumbers> <xsl:for-each select="Customer/Address/ContactNumber/*[contains(local-name(),'PhoneNumber')]"> <xsl:value-of select="." /> <xsl:if test="position() != last()"> <xsl:value-of select="$comma" /> </xsl:if> </xsl:for-each> </ContactNumbers> <PostalCode><xsl:value-of select="Customer/Address/PostalCode" /></PostalCode> <ItemName><xsl:value-of select="Name" /></ItemName> <ItemType><xsl:value-of select="Type" /></ItemType> <Author><xsl:value-of select="Author" /></Author> <list-price><xsl:value-of select="ListPrice" /></list-price> <sell-price><xsl:value-of select="SellPrice" /></sell-price> <cost-price><xsl:value-of select="Cost" /></cost-price> <profit><xsl:value-of select="format-number(SellPrice - Cost, '0.##')" /></profit> </xsl:copy> </xsl:template> </xsl:stylesheet>

driver.findElement(By.xpath("//xpath")).click();

这是html:

driver.findElement(By.xpath("//div[@id='container']/div[@id='main']/div[@id='columnOne']/div[@class='ng-scope']/div[@class='content ng-scope']/div/div[@class='ng-isolate-scope']/div[@class='tab-content']/div[@class='tab-pane ng-scope active']/div[@class='ng-scope']/div[@id='uploadRequests']/div[@id='fileUploadPanel']/div[@class/span[@class='btn btn-primary']='panel-body']/div/div[@class='panel panel-default']/div[@class='panel-heading clearfix']/div[@class='pull-left']/label")).click();

我通常在By.xpath之前尝试 By.className(className),By.id(id),By.name(name),By.linkText(linkText),但即使在这里作为我登录网站后无法点击“添加”文件按钮。

1 个答案:

答案 0 :(得分:-1)

尽量避免使用xpath。当没有可能使用其他选择器(如css选择器或类)时,情况非常少。此外,没有足够的HTML代码来找出xpath不起作用的原因。请添加更多信息。

PS试试这个:

driver.findElement(By.xpath("//input[@id='inputFile']/span[@class='btn btn-primary']")).click();