Discovering elements using the XPath in Selenium WebDriver

Imagine you have a website:

  <div class="formfonttitle">Wireless - General</div>
    <div style="margin-left:5px;margin-top:10px;margin-bottom:10px"><img       src="/images/New_ui/export/line_export.png"></div>
    <div class="formfontdesc">Set up the wireless related information below.</div>
    <table width="99%" border="1" align="center" cellpadding="4" cellspacing="0" id="WLgeneral" class="FormTable">
    <tr id="wl_unit_field">
    <th>Frequency</th>
    <td>
    <select name="wl_unit" class="input_option" onChange="_change_wl_unit(this.value);">
    <option class="content_input_fd" value="0" >2.4GHz</option>
    <option class="content_input_fd" value="1" selected>5GHz</option>
    </select>
    </td>
    </tr>

You are attempting to choose "2.4GHz" using xpath. However, the default option is "5GHz." You are executing this task through a Python script with the help of the Selenium webdriver.

Your approach involves:

elements = mydriver.find_element_by_xpath("//div[@class='wl_unit']")
title = elements[1].find_elements_by_xpath(".//div[@class='content_input_fd']")

Unfortunately, the above method is not producing the desired outcome.

Answer №1

To locate and select the 2.4GHz option element using XPath, you can utilize the code snippet below :

element = mydriver.find_element_by_xpath("//select[@name='wl_unit']/option[@value='0']")
element.click()

Answer №2

To achieve this, you can execute JavaScript using selenium in the following manner:

myDriver.execute_script("$('.input_option option[value="0"]')")

Here is a helpful resource that can assist you in selecting an option using Python Selenium.

Answer №3

@Samantha If you want to reference a dropdown element using Selenium, you can do so by using the select method. Here's an example:

Here's your initial code snippet:

WebElement dropdownElement = myDriver.findElement(By.xpath("//div[@class='wl_unit']"));

Now, you can create a new Select object:

Select dropdown = new Select(dropdownElement);

dropdown.selectByValue(1);

or

dropdown.selectByVisibleText("5GHz");

This should assist you with your task. Let me know if you need further clarification.

Answer №4

Check out this code snippet:

WebElement unitElement = driver.findElement(By.name("unit"));
Select selectUnit = new Select(unitElement);
selectUnit.selectByValue("0");

Alternatively, you can use:

WebElement unitElement = driver.findElement(By.name("unit"));
Select selectUnit = new Select(unitElement);
selectUnit.selectByVisibleText("2.4GHz");

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

Having trouble getting jQuery .click to trigger on a dynamically loaded button?

I'm having some trouble with my code. I have a list that loads dynamically with a button inside, and although I have set up jQuery to handle the click event, nothing is happening. Here's a snippet of the code: HTML: <ul id="cart"> ...

What is the best way to ensure these 2 divs are aligned vertically in the provided html (starting at the same vertical level)?

<div class="container"> <div class="row"> <div class="col-lg-12"> <div class="news-title"> <h3 class="title">NEWS & ARTICLES</h3> ...

JavaScript Code: Empty a text box when a different one is active

Looking for a solution to clear the content of one textbox when the user interacts with another in HTML and JavaScript. <input id="tb1" type="text" name="textbox1"> <input id="tb2" type="text" name="textbox2"> Seeking JavaScript code that wil ...

The jQuery carousel script is not functioning

Currently developing a jQuery slideshow for my website, and I stumbled upon a code that fits my requirements perfectly. I made some adjustments to the code from this source: . My specifications were that the code should be created from scratch, lightweight ...

Create a sleek transition for your Bootstrap 4 fixed Navbar by having it smoothly slide down and change to a

Throughout my experience with HTML/CSS, I have predominantly relied on themes that included this feature by default. However, as I now venture into building from scratch, I find myself struggling to recreate it. You'll notice that I have a stylish fi ...

Changing Content Dynamically in Adobe Muse

I'm in the process of creating a website for an internet radio station and I've chosen to use Adobe Muse due to its user-friendly design features. My goal is to have the header and footer sections stay static while only the content on the page ch ...

I would like to retrieve my data using my personal API and have them displayed as checkboxes

https://i.sstatic.net/qPSqe.jpgHere is an excerpt of the progress I have made main.html (it's actually a form) <div class="form-group form-check-inline"> <input class="form-check-input" type="radio" name=& ...

How can you center a div at the top of another div?

I am working with a nested div structure and trying to center the inner content of one div within another. Is it possible to achieve this using CSS? <div id="outer"> bla, bla....... <div id="inner"> <p>Test</p> </div> ...

Incorporating the fadeout technique in conjunction with the append() function

Here is some code that I am working with: $('body').on('click' , function(){ $('body').append('<div class="ajax-success"><p>Its Done !!!</p></div>').fadeOut(2000); }); The goal was to a ...

Press a sequence of buttons repeatedly

I am looking to automate my Angular application using Selenium Web Driver. I have implemented a code snippet to handle a series of dialog windows with OK buttons: // Check if Survey exit window is displayed and click OK button clickConfirmWindow(driver, SU ...

What are some ways I can efficiently load large background images on my website, either through lazy loading or pre

Just dipping my toes into the world of javascript. I'm currently tackling the challenge of lazy loading some large background images on my website. My goal is to have a "loading" gif displayed while the image is being loaded, similar to how it works ...

I'm having trouble getting my HTML <button> element to function correctly

I'm currently working on a project where I need to animate a green bar moving upward over a blue bar when a button is clicked. However, no matter what I try, the button doesn't seem to be working. I've experimented with using both the button ...

The email validation function is not functioning correctly when used in conjunction with the form submission

I'm currently working on my final project for my JavaScript class. I've run into a bit of a roadblock and could use some guidance. I am trying to capture input (all code must be done in JS) for an email address and validate it. If the email is va ...

Troubles with Sizing in Twitter Bootstrap Modal

One issue I have encountered with my application is the varying widths of modals. Setting the width inline works well, but when the window is compressed it can cause part of the modal to be off screen, rendering it useless at smaller resolutions such as th ...

Running tests in parallel with Selenium, Java, and TestNG has been posing some unusual challenges

After successfully running my test (not in parallel), I encountered strange issues when attempting to run the same test twice. The behavior exhibited included a blank page or both instances closing unexpectedly, among other unexplainable occurrences. It ap ...

I encountered an issue where my style.css file was not properly linking to my HTML code. Whenever I saved the file, it would open

I'm a beginner in the world of HTML/CSS. When I save my style.css file, it shows up as a Notepad+ settings file and doesn't seem to work with my HTML. Can anyone advise me on what steps I should take? ...

Ways to decrease the saturation of a background image within a div using CSS

I've come across many plugins that can desaturate an image within the tag. However, I haven't been able to find a solution for desaturating a background image that is used for an element (div). Is there a way to desaturate the background image ...

Angular element fails to display properly

I'm currently working on developing a website using Angular and creating a header component. To generate the necessary files, I used the command ng g c commons/header which creates the HTML, SCSS, TS, and .spec.ts files. I then made modifications to t ...

Modify Knockout applyBindings to interpret select choices as numeric values

Utilizing Knockout alongside html select / option (check out Fiddle): <select data-bind="value: Width"> <option>10</option> <option>100</option> </select> Upon invoking applyBindings, the options are interprete ...

Update the content according to the size of the screen

I'm currently working on a project where I need to redirect pages based on screen sizes. I have separate index files for the mobile and web versions of the website. In the web version's index file, I've added the following script: <scri ...