如何在Selenium C中勾选一个带有url链接的文本的复选框#

时间:2016-03-03 16:03:23

标签: c# selenium-webdriver chrome-web-driver

这是我试图访问的代码

<div class="field checkbox">
  <label class="radio">
    <input payoneer="CheckBox" type="checkbox" name="ctl00$plcMainArea$SignDocument_4_2" validate="validate" data-rule-required="true" data-msg-required="This field is required." hidefocus="hidefocus" class="field-data" id="SignDocument_4_2" data-sign-document-type="4_2">&nbsp;I agree to the
    <a href="pagecontent.payoneer?rsckey=ProcessorTypeDebitCard.ElectronicDisclosure&amp;pid=Y0LwV0AQC7cY%2b5AIqsZl1g%3d%3d" target="_blank">Electronic Disclosures</a>
    <a href="pagecontent.payoneer?rsckey=ProcessorTypeDebitCard.Privacy&amp;pid=Y0LwV0AQC7cY%2b5AIqsZl1g%3d%3d" target="_blank">Privacy Policy</a>
  </label>
</div>

我试图通过ID找到元素并点击其上的复选框

driver.FindElement(By.Id("SignDocument_4_2")).Click();

这是我得到的例外

System.InvalidOperationException was unhandled
  HResult=-2146233079
  Message=unknown error: Element is not clickable at point (257, 955). Other element would receive the click: <a href="pagecontent.payoneer?rsckey=ProcessorTypeDebitCard.ElectronicDisclosure&amp;pid=Y0LwV0AQC7cY%2b5AIqsZl1g%3d%3d" target="_blank">...</a>
  (Session info: chrome=48.0.2564.116)
  (Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 6.1 SP1 x86_64)
  Source=WebDriver
  StackTrace:
       at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
       at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
       at OpenQA.Selenium.Remote.RemoteWebDriver.InternalExecute(String driverCommandToExecute, Dictionary`2 parameters)
       at OpenQA.Selenium.Remote.RemoteWebElement.Execute(String commandToExecute, Dictionary`2 parameters)
       at OpenQA.Selenium.Remote.RemoteWebElement.Click()
       at Payoneer.Exam.Main(String[] args) in c:\Users\Alberto Chocron\Documents\Visual Studio 2013\Projects\PayoneerTest\Payoneer\Program.cs:line 105
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

2 个答案:

答案 0 :(得分:1)

错误Element is not clickable at point (257, 955). Other element would receive the click表示您要点击的元素对Selenium不可见。要模拟元素的移动,请使用MoveToElement

中的Actions
IWebElement checkbox = driver.FindElement(By.Id("SignDocument_4_2"));
Actions actions = new Actions(driver);
actions.MoveToElement(checkbox).perform();
checkbox.Click();

您也可以尝试点击复选框父母

driver.FindElement(By.ClassName("checkbox")).Click();
// or
driver.FindElement(By.ClassName("radio")).Click();

另一种选择是使用java脚本点击

IWebElement checkbox = driver.FindElement(By.Id("SignDocument_4_2"));
IJavascriptExecutor executor = (IJavascriptExecutor)driver;
executor.ExecuteScript("arguments[0].click();", checkbox);

答案 1 :(得分:0)

首先,看看你正在使用正确的try和catch块,否则方法会被正确抛出,其次,实际上没有检测到这一点。所以试试

            string checkboxXPath = "//input[contains(@id, SignDocument_4_2')]"
            IWebElement elementToClick = driver.FindElement(By.XPath(checkboxXPath));
            elementToClick.Click();