asp.net RadioButton始终对硒显示为false

时间:2019-02-22 13:21:11

标签: c# asp.net selenium selenium-webdriver

我有一个如下的UI,我正在尝试使它自动化。

enter image description here

我的html内容如下:

<div class="medium-5 columns tableheading-td">
  <ul id="ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers" class="assessment-main-layout tabled-list" onchange="ChangeTextColorForSelectedRadio(this);">
    <li>
      <input id="ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_0" type="radio" name="ctl00$ctl00$ContentPlaceHolder1$QuetionListPlaceHolder$BigFiftyPersonalityQuestion1$BigFiftyPersonalityQuestionItem1221754$rdbListAnswers"
        value="0">
      <label for="ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_0"> </label>
    </li>
    <li>
      <input id="ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_1" type="radio" name="ctl00$ctl00$ContentPlaceHolder1$QuetionListPlaceHolder$BigFiftyPersonalityQuestion1$BigFiftyPersonalityQuestionItem1221754$rdbListAnswers"
        value="1">
      <label for="ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_1"> </label>
    </li>
    <li>
      <input id="ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_2" type="radio" name="ctl00$ctl00$ContentPlaceHolder1$QuetionListPlaceHolder$BigFiftyPersonalityQuestion1$BigFiftyPersonalityQuestionItem1221754$rdbListAnswers"
        value="2" style="">
      <label for="ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_2"> </label>
    </li>
    <li>
      <input id="ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_3" type="radio" name="ctl00$ctl00$ContentPlaceHolder1$QuetionListPlaceHolder$BigFiftyPersonalityQuestion1$BigFiftyPersonalityQuestionItem1221754$rdbListAnswers"
        value="3">
      <label for="ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_3"> </label>
    </li>
    <li>
      <input id="ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_4" type="radio" name="ctl00$ctl00$ContentPlaceHolder1$QuetionListPlaceHolder$BigFiftyPersonalityQuestion1$BigFiftyPersonalityQuestionItem1221754$rdbListAnswers"
        value="4">
      <label for="ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_4"> </label>
    </li>
  </ul>
</div>

我尝试按以下步骤单击我的单选按钮:

IWebElement webElement= CommonProperties.driver.FindElement(By.Id("ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_2"));
webElement.ClickElement();

但它表示element显示为false。 他的原因是什么,我该如何解决? enter image description here

我尝试使用xpath:

 IWebElement webElement= CommonProperties.driver.FindElement(By.XPath("//*[@id='ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_0']"));

(具有两个xpath用于单选按钮和标签。即使遇到相同的问题。

//*[@id="ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_0"]

//*[@id="ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers"]/li[1]/label
  

OpenQA.Selenium.ElementNotVisibleException HResult = 0x80131500
  消息=元素不可见(会话信息:chrome = 72.0.3626.109)
  (驱动程序信息:chromedriver = 2.37.544315   (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),平台= Windows NT   10.0.10586 x86_64)Source = WebDriver

     

StackTrace:位于OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response   errorResponse)   OpenQA.Selenium.Remote.RemoteWebDriver.Execute(字符串   driverCommandToExecute,字典2 parameters) at OpenQA.Selenium.Remote.RemoteWebElement.Execute(String commandToExecute, Dictionary 2个参数)位于   OpenQA.Selenium.Remote.RemoteWebElement.Click()在   SeleniumBL.SeleniumSetMethods.ClickElement(IWebElement element)在   D:\ SourceControl \ Test \ Test   \ SeleniumHR \ SeleniumHR \ SeleniumBL \ SeleniumSetMethods.cs:line   53岁   SeleniumHR.Models.Assesment.BigFiftyAssesment.AssesmentPlay()在   D:\ SourceControl \ Test \ Test   \ SeleniumHR \ SeleniumHR \ SeleniumHR \ Models \ Assesment \ BigFiftyAssesment.cs:line   47在SeleniumHR.Models.AssesmentBase.AssesmentStart()中   D:\ SourceControl \ Test \ Test   \ SeleniumHR \ SeleniumHR \ SeleniumHR \ Models \ Assesment \ AssesmentBase.cs:line   216点   SeleniumHR.Models.Assesment.BigFiftyAssesment.ExecuteAssesment()在   D:\ SourceControl \ Test \ Test   \ SeleniumHR \ SeleniumHR \ SeleniumHRM \ Models \ Assesment \ BigFiftyAssesment.cs:line   24点   SeleniumHR.Models.Assesment.AssesmentFactory.AssesmentPlay(String   D:\ SourceControl \ Test \ Test中的AssesmentType)   \ SeleniumHRM \ SeleniumHR \ SeleniumHR \ Models \ Assesment \ AssesmentFactory.cs:line   48在SeleniumHRM.Program.Main(String [] args)中   D:\ SourceControl \ Test \ Test   \ SeleniumHR \ SeleniumHR \ SeleniumHR \ Program.cs:第49行

我使用过的Nuget如下: enter image description here

3 个答案:

答案 0 :(得分:1)

exception details : element not visible (Session info: chrome=72.0.3626.109) (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 10.0.10586 x86_64)

这表明您使用的chromedriver=2.37.544315较旧版本与chrome version 72不兼容。您需要升级chrome驱动程序以使其兼容。我相信这会解决您的问题。

请参阅以下link

答案 1 :(得分:1)

您能否尝试使用 visibilityOfElementLocated 来等待元素单击,然后单击

WebDriverWait wait = new WebDriverWait(driver, 10);
var clickableElement = wait.Until(ExpectedConditions.visibilityOfElementLocated(By.XPath("//*[@id='ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_0']")));

答案 2 :(得分:0)

我能够找出我的问题。根据样式类,单选按钮被禁用。因此,它始终显示为false。我可以使用相关标签文本单击来单击单选按钮。我做到了

 // get lable for selected radio button
IWebElement radioButtonSelectedLabel = CommonProperties.driver.FindElement(By.CssSelector("label[for='ContentPlaceHolder1_QuetionListPlaceHolder_BigFiftyPersonalityQuestion1_BigFiftyPersonalityQuestionItem1221754_rdbListAnswers_0']"));
// click on the label
radioButtonSelectedLabel.ClickElement();