XPath: The element seems to be absent from the DOM, yet it still exists in some form or another

Struggling to check this checkbox using XPath, but every attempt fails as it accidentally clicks on the link in the legend (Lorem ipsum)


https://i.sstatic.net/v3Wmz.png


HTML code:


https://i.sstatic.net/dyaL0.png


<div class="input-outer checkbox">
   <div class="section-head sec-head2">
      <!---->
      <div class="pull-left">Tick this box</div>
      <!----><!---->
      <p aria-label="required" class="requiredAsteric">*</p>
   </div>
   <div class="input-validation">
      <div class="input-icon-outer">
         <!---->
         <!---->
         <!---->
         <!---->
         <input class="filled-in ng-untouched ng-pristine ng-invalid" type="checkbox" aria-label="Acceptera villkor" aria-required="true" aria-describedby="err_icon_prefix2_1" id="icon_prefix2_1"><!---->
         <label class="leadform-checkbox" tabindex="0" for="icon_prefix2_1">
            <div>
               <p>Lorem ipsum dolor sit amet, consectetur  <a href="https://www.google.com" target="_blank">adipiscing elit</a> adipiscing elit , sed do <a href="https://www.stackexchange.com" target="_blank">eiusmod tempor </a> incididunt ut labore et dolore magna aliqua.</p>
            </div>
         </label>
         <!---->
      </div>
   </div>
</div>

All attempts with these XPaths have been unsuccessful.

//label[@class='leadform-checkbox']/div";
//input[@aria-label='Tick this box']";
(//div[@class='input-icon-outer'])[3]";

Seems like the checkbox doesn't use the standard HTML functionality, rather a custom CSS-class that resembles a checkbox. Clicking on the div sends the click to the text content instead of the checkbox, leading to the accidental link click.

Is there a fix for this? How do I direct the click to the square?

Edit: Other fields in the "dialog" are accessible with these XPaths:

String xpText = "//input[@aria-label='text']";
String xpNumber = "//input[@aria-label='number']";

All elements are within the same iframe. Multiple switches between the main content and various iframes were done prior.

The heading 2.2 is visible at the top of the screenshot from the HTML source above, indicating correct frame/content selection. Also, links within the text related to this checkbox were accessible.

Included the complete dialog below for easier reference:


https://i.sstatic.net/I1NDv.png

Answer №1

//input[@aria-label='Check this box']
is the solution you're looking for.

Answer №2

Ensure that when inspecting the element, you are referencing the correct line in the HTML code that highlights the desired element. If this approach fails, double-check for the presence of an <iframe on the page and consider leveraging parent/child relationships to pinpoint the target element. I recommend trying: //div[@class='input-outer checkbox'] or //div[@class='section-head sec-head2']

Answer №3

To interact with the checkbox, you have a choice of using various XPath-based Locator Strategies:

  • Find the checkbox by <label> tag attribute:

    driver.find_element_by_xpath("//label[@for='icon_prefix2_1']").click()
  • Locate the checkbox using the <p> tag attribute:

    driver.find_element_by_xpath("//label[@class='leadform-checkbox'][.//p[contains(., 'Lorem ipsum dolor sit amet')]]//preceding::input[1]").click()
  • Identify the checkbox by its <input> tag attributes:

    driver.find_element_by_xpath("//input[@class='filled-in ng-untouched ng-pristine ng-invalid' and @aria-label='Acceptera villkor']").click()

For successful interaction with the element, it is recommended to use WebDriverWait for element_to_be_clickable(). You can choose from the following XPath-based Locator Strategies:

  • Utilize the <label> tag attribute:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='icon_prefix2_1']"))).click()
  • Use the <p> tag attribute:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@class='leadform-checkbox'][.//p[contains(., 'Lorem ipsum dolor sit amet')]]//preceding::input[1]"))).click()
  • Employ the <input> tag attribute:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='filled-in ng-untouched ng-pristine ng-invalid' and @aria-label='Acceptera villkor']"))).click()
  • Note: Don't forget to include the necessary imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I add a variable to an object in Three.js?

Creating an object in Three.js and adding a variable seems like a simple task, but I'm struggling to figure it out. For instance, I want to create a cube and have it store a variable called "beenHit" that can be accessed later on. var geometry = new ...

Issue with Angular Factory not being invoked

I am currently using a tutorial to create a MEAN app with Google Maps. However, I have encountered an issue where the map is not appearing on the page. Surprisingly, there are no errors in the browser console and even when debugging with node-inspector, I ...

Error: SEC_ERROR_UNKNOWN_ISSUER encountered while using Selenium-wire with Firefox

While attempting to access a certain website and retrieve its network headers, I keep encountering the error message "SEC_ERROR_UNKNOWN_ISSUER". Surprisingly, there is no option to click on "Accept the Risk and Continue", only a button to go back. I&apos ...

"Integrating JavaScript files into HTML using Flask: A step-by-step guide

I have created an HTML page with a JavaScript section where I am importing various other .js files: <script type="module"> import * as THREE from "../build/three.module.js"; import { OrbitControls } from ...

selenium script execution encountered an error

Having trouble executing the script on the page Here is the HTML of the page: https://i.sstatic.net/kympU.jpg And here is the script code: pprint(wd.execute_script("return getChartInitJsCode_5f9462fb094a4()")) The full code snippet includes: ...

The IF ELSE condition in Python Selenium WebDriver fails to execute the ELSE block when the specified element is missing

Using an automation script with selenium web driver has been mostly successful, but occasional changes in page content have caused issues. When the script fails to find the expected element, it crashes instead of executing the alternative else statement. ...

When incorporating express.static(), the Express .use() callback may be triggered multiple times

I'm in the process of verifying a user's identity, and once that is confirmed I aim to add them as a new user in my personal database using the information provided by the authentication server. The issue at hand is that the function createNewAc ...

Is it feasible to conceal certain parameters within a URL using Angular UI Router?

Looking to pass two values to a new ui-view via parameters: item id list of objects However, I want the new view to display only the id in the browser URL and not the stringified array of objects: http://www.myapp.com/#/my-view/4 INSTEAD OF http://ww ...

Error: The PyCharm is unable to locate the imported file for SeleniumLibrary

I'm still fairly new to using pycharm, robotframework, and selenium. I've searched through all the questions here but haven't been able to find a solution to my problem. Everything was working fine with my scripts 2 weeks ago, but now I kee ...

Selecting and combining arrays in JavaScript

I am faced with a dataset that has the following structure: [ { ProductID: 1, ProductName: 'MyProduct', Description: '.. some text here ..', UnwantedData: 'garbage here' }, { ...

The sluggish loading speed of the page is being caused by both jQuery and an external file containing Amazon

I am facing an issue with my website where it is loading over 1000 images per page from Amazon servers. To enhance the functionality, I have integrated jQuery plugins that are stored locally on the webserver, without using any remote JS or CSS. However, ...

SSR with Material UI Drawer encounters issue during webpack production build meltdown

When I attempted to utilize the Material UI SSR example provided by Material-UI (Link), it worked successfully. However, my next step was to integrate the Material-UI Drawer into this example. To do so, I utilized the Persistent drawer example code also pr ...

Modify the database value linked to an <input> element each time its value is modified

I attempted to create something similar to the example provided here $(document).ready(function(){ $('input[type=text]').keyup(function(){ var c=0; var a=$(this).attr('name'); //a is string //if var a change.. ...

Tips for sequentially arranging and rearranging an array of numbers, even when duplicates are present

Encountered a perplexing issue that has me scratching my head in an attempt to visualize a solution. Currently, I am working with an array of objects that appears as follows: let approvers = [{order:1, dueDate: someDate},{order:2, dueDate: someDate}, ...

I've been working on a script in JavaScript to retrieve lectures for a specific day, but I'm having trouble connecting my jQuery code to it

Exploring a JavaScript function that retrieves today's lectures for a specific section of a class using jQuery. The challenge lies in implementing this array of classes on an HTML file. //function to get today var today = new Date(); var dd = today ...

Having trouble obtaining error message in selenium using Java and JUnit

Having trouble capturing error messages that are being displayed? I've tried multiple methods, but each one seems to throw an exception with the message "unable to find element." Can someone please assist by providing the code needed for this task? Th ...

Place a user input field within a div element having the specified class

I am trying to insert an HTML input element into the following div: <div class="sss" style="padding-top:2px"> <center> <input value="test1" name="name1" type="submit" > <input value="test2" name="name2" type="submit"> </c ...

Utilizing Angular.JS to implement a template featuring tags

My web application built with AngularJS includes <p> tags. I am looking to apply a template from a server file to the innerHTML of these <p> tags. The template consists of simple text with various tags like <b>, <ol>, and other text ...

Tips for ensuring a method is not invoked more than once with identical arguments

I'm grappling with a challenge in JavaScript (or typescript) - ensuring that developers cannot call a method multiple times with the same argument. For instance: const foo = (name: string) => {} foo("ABC") // ok foo ("123") ...

Error encountered: Unexpected token '"', expecting "}". Perspective from React : It is an unfortunate occurrence which h

I'm a newbie when it comes to React and I'm currently following a tutorial. Unfortunately, I encountered an error message while the script was trying to compile: Parsing error: Unexpected token, expected "}" The issue seems to be with the &apos ...