简体   繁体   English

无法定位元素:{"method":"css selector","selector":"#id_gender1"}

[英]Unable to locate element: {"method":"css selector","selector":"#id_gender1"}

I'm trying to select a checkbox / radio button by all the possible ways that I can think of, but none of them have worked.我试图通过我能想到的所有可能的方式来选择一个复选框/单选按钮,但它们都没有奏效。

This is the way I am trying to identity de checkbox这是我试图识别复选框的方式

WebElement selectGender = driver.findElement(By.id("id_gender1"));
selectGender.click();

Here is the HTML code这是 HTML 代码

<div class="radio-inline">
    <label for="id_gender1" class="top">
     <div class="radio" id="uniform-id_gender1"><span class="checked"><input type="radio" name="id_gender" id="id_gender1" value="1">
     </span>
     </div>
       Mr.
   </label>
</div>

This is the error message I am receiving.这是我收到的错误消息。

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#id_gender1"}
  (Session info: chrome=75.0.3770.142)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'JEFFERSONPC', ip: '192.168.100.6', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_211'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.142, chrome: {chromedriverVersion: 75.0.3770.140 (2d9f97485c7b..., userDataDir: C:\Users\JEFFER~1\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:63666}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 65163859269265511fa3118a5c9b1f79
*** Element info: {Using=id, value=id_gender1}

Induce WebDriverWait and elementToBeClickable to click on the element.诱导WebDriverWaitelementToBeClickable点击元素。

Try the following options.尝试以下选项。

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement selectGender = wait.until(ExpectedConditions.elementToBeClickable(By.id("id_gender1")));
selectGender.click()

OR或者

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement selectGender = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='radio-inline']/label[@for='id_gender1']")));
selectGender.click()

这是你的代码

  findElement(By.xpath("//input[@name='id_gender']")).click();

--Disclaimer: cant comment yet due to lack of reputation-- --免责声明:由于缺乏声誉,目前无法发表评论--

the code where you fetch the id seems fine, and no spelling errors in the code either.获取 id 的代码看起来不错,代码中也没有拼写错误。 But it could be that your answer is found here:但您的答案可能在这里找到:

Unable to locate an Element By ID in Selenium 无法在 Selenium 中通过 ID 定位元素

Also: please post what you have tried so far in a bit more detail.另外:请更详细地发布您迄今为止尝试过的内容。 You say in all ways you know, but we dont know what that is :).你用你知道的所有方式说,但我们不知道那是什么:)。

For example, reference the stackoverflow answers you have tried that are similar to yours例如,参考您尝试过的与您相似的 stackoverflow 答案

Your code works fine.你的代码工作正常。 Why your error message have css Selector?为什么你的错误信息有 css Selector? when you are not using cssSelector.当您不使用 cssSelector 时。

if using cssSelector then: WebElement selectGender = driver.findElement(By.cssSelector("#id_gender1"));如果使用 cssSelector 则: WebElement selectGender = driver.findElement(By.cssSelector("#id_gender1")); selectGender.click(); selectGender.click();

I am sure you are trying to click element which is inside the frame.我确定您正在尝试单击框架内的元素。 First of all you have to switch driver on that frame and perform your test script.首先,您必须在该框架上切换驱动程序并执行您的测试脚本。

driver123.switchTo().frame(<some frame index or id>;//Switching frame
WebElement selectGender = driver.findElement(By.id("id_gender1"));//Storing element
selectGender.click();//Clicking on element

Note: If you do not have frame id or index use xpath for switch to that frame.注意:如果您没有帧 ID 或索引,请使用 xpath 切换到该帧。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法找到元素:{“方法”:“ id”,“选择器”:“ sysPublishDateDate”} - Unable to locate element: {“method”:“id”,“selector”:“sysPublishDateDate”} 没有这样的元素:无法找到元素:{"method":"css selector","selector":"input#btnshow"} - no such element: Unable to locate element: {"method":"css selector","selector":"input#btnshow"} 无法找到元素:{“方法”:“名称”,“选择器”:“ markUp” - Unable to locate element: {“method”:“name”,“selector”:“markUp” 线程“ main”中的异常org.openqa.selenium.NoSuchElementException:无此类元素:无法找到元素:{“ method”:“ id”,“ selector”:“ name”} - Exception in thread “main” org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:“id”,“selector”:“name”} 使用 Selenium 和 Java 时无法定位元素:{“method”:“xpath”,“selector”:“//li[@id=”tablist1-tab3“]”} 错误 - Unable to locate element: {“method”:“xpath”,“selector”:“//li[@id=”tablist1-tab3“]”} error using Selenium and Java 无法在Selenium中找到元素尝试过的xpath,name,id,css选择器 - Not able to locate element in Selenium tried xpath,name,id,css selector 无法使用类名称,xpath或CSS选择器定位元素 - Unable to locate element with class name, xpath or CSS selector Selenium WebDriver:Java:NoSuchElementException:无法找到元素:{“ method”:“ xpath”,“ selector”:“ // div [@ id =&#39;manage_area&#39;] / ul / li / div [2]”} - Selenium WebDriver: Java: NoSuchElementException: Unable to locate element: {“method”:“xpath”,“selector”:“//div[@id='manage_area']/ul/li/div[2]”} Selenium Java Webdriver,NoSuchElementException:没有这样的元素:无法找到元素:{“ method”:“类名”,“ selector”:“ btn_content”} - Selenium Java Webdriver, NoSuchElementException: no such element: Unable to locate element: {“method”:“class name”,“selector”:“btn_content”} NoSuchElementException:否这样的元素:无法找到元素:{“ method”:“ xpath”,“ selector”:“ html / body / form / input [1]”} - NoSuchElementException: no such element: Unable to locate element: {“method”:“xpath”,“selector”:“html/body/form/input[1]”}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM