Discover the combobox element and its value can be easily achieved by using Selenium's find_element_by_xpath method

I am having trouble setting a value in a combobox using selenium. The find_element_by_xpath statement is failing to locate the combobox by class or ng-model. Specifically, I am attempting to change the time period for a stock from one day to one week.

Here is the relevant javascript code on the webpage:

**<select ng-model="analysisGraph.periodselector.period" class="ng-valid ng-dirty ng-valid-parse user-success ng-touched">**
    <!-- ngIf: !analysisGraph.periodselector.dayOnly -->
    <option ng-if="!analysisGraph.periodselector.dayOnly" value="INTRADAY" class="ng-binding ng-scope">Intradag</option><!-- end ngIf: !analysisGraph.periodselector.dayOnly --><!-- ngIf: !analysisGraph.periodselector.dayOnly -->
    <option ng-if="!analysisGraph.periodselector.dayOnly" value="ONE_WEEK" class="ng-binding ng-scope">1 uke</option><!-- end ngIf: !analysisGraph.periodselector.dayOnly -->
        <option value="ONE_MONTH" class="ng-binding">1 mnd</option><!-- ngIf: !analysisGraph.periodselector.hideOption.hide3m -->
    <option ng-if="!analysisGraph.periodselector.hideOption.hide3m" value="THREE_MONTHS" class="ng-binding ng-scope">3 mnd</option><!-- end ngIf: !analysisGraph.periodselector.hideOption.hide3m --><!-- ngIf: !analysisGraph.periodselector.hideOption.hide6m -->
    <option ng-if="!analysisGraph.periodselector.hideOption.hide6m" value="SIX_MONTHS" class="ng-binding ng-scope">6 mnd</option><!-- end ngIf: !analysisGraph.periodselector.hideOption.hide6m -->
        <option value="YTD" class="ng-binding">YTD</option><!-- ngIf: !analysisGraph.periodselector.hideOption.hide1y -->
    <option ng-if="!analysisGraph.periodselector.hideOption.hide1y" value="ONE_YEAR" class="ng-binding ng-scope">1 år</option><!-- end ngIf: !analysisGraph.periodselector.hideOption.hide1y --><!-- ngIf: !analysisGraph.periodselector.hideOption.hide3y -->
    <option ng-if="!analysisGraph.periodselector.hideOption.hide3y" value="THREE_YEARS" class="ng-binding ng-scope">3 år</option><!-- end ngIf: !analysisGraph.periodselector.hideOption.hide3y --><!-- ngIf: !analysisGraph.periodselector.hideOption.hide5y -->
    <option ng-if="!analysisGraph.periodselector.hideOption.hide5y" value="FIVE_YEARS" class="ng-binding ng-scope">5 år</option><!-- end ngIf: !analysisGraph.periodselector.hideOption.hide5y --><!-- ngIf: hasLaunchData -->
</select>

Here is the code I have attempted:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from bs4 import BeautifulSoup
import pymysql
import ose_data

chrome_options = Options()
driver = webdriver.Chrome(executable_path='chromedriver', options=chrome_options)
driver.get('https://www.oslobors.no/markedsaktivitet/#/details/ADE.OSE/overview')
time.sleep(5)
bs = BeautifulSoup(driver.page_source, 'html.parser')
**driver.find_element_by_xpath("//select[@class='ng-valid ng-dirty ng-valid-parse user-success ng-touched']/option[text()='ONE_WEEK']").click()**

However, this results in the following error:

Error: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//select[@class='ng-valid ng-dirty ng-valid-parse user-success ng-touched']/option[text()='ONE_WEEK']"} (Session info: chrome=79.0.3945.130)

I also tried searching with driver.find_element_by_xpath("//select[@ng-model='analysisGraph.periodselector.period']/option[text()='ONE_WEEK']").click()

I am a beginner Python programmer, can anyone spot any obvious errors?

Answer №1

The Xpath provided is incorrect, instead use the

select[ng-model="analysisGraph.periodselector.period"]
CSS selector to locate the element. This element is a dropdown with a select tag. To interact with it, utilize the Select class. Additionally, include WebDriverWait to ensure the element is clickable before interacting with it.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from bs4 import BeautifulSoup
import pymysql
import ose_data
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select

chrome_options = Options()
driver = webdriver.Chrome(executable_path='chromedriver', options=chrome_options)
driver.get('https://www.oslobors.no/markedsaktivitet/#/details/ADE.OSE/overview')
wait = WebDriverWait(driver, 10)

# bs = BeautifulSoup(driver.page_source, 'html.parser')
details_period = Select(wait.until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, 'select[ng-model="analysisGraph.periodselector.period"]'))))
details_period.select_by_value('ONE_WEEK')

# You can also use
# details_period.select_by_visible_text()
# details_period.select_by_index()

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

What is the best way to delete the "Click to sort Ascending" text from the header of each column in a bootstrap-vue table?

Recently, I came across a bootstrap-vue table that caught my attention: https://i.sstatic.net/5jENs.png Below is the code snippet for the table setup: <template> <div class="container"> <h1 class="pt-2 pb-3">Bo ...

Creating a Cross Fade Animation effect with the combination of CSS and JavaScript

I've been attempting to create a similar animation using html and css. Below gif shows the desired outcome I am aiming for: https://i.sstatic.net/YsNGy.gif Although I have tried the following code, I have not been able to achieve the desired result ...

Clicking on the button has no effect whatsoever

I'm currently dealing with a button on my webpage that seems to be causing me some trouble: <script> function changeMap() { container.setMap(oMap); } </script> <button onClick="changeMap"> Click here </button> Upon inspe ...

Is there a way to sort a table using a dropdown menu selection as a filter?

I am currently working on a project that involves a table with three columns, which are typically populated from a SQL database. My goal is to filter the third column based on the value selected from a dropdown menu. I came across a helpful tutorial on W3 ...

Customize the background color of highlighted text using HTML and jQuery

Recently, I modified an existing code to divide plain text into four classes by selecting a portion of the text and coloring it. Afterwards, I extracted the text of each class and stored it in my database. While this code works well, I am looking for a way ...

Is there a feature similar to Nuxt.js' auto-register in Next.js?

My Journey as a Beginner Being a beginner in the tech world, specifically in full-stack development (although I'm about 8 years behind), I find myself grappling with the decision of what to focus on learning. Vue and Nuxt.js are fantastic technologi ...

Manually sending the form via Ajax for submission

I'm facing an issue where I am trying to utilize ajax to call a servlet upon form submission. However, the ajax call is not being triggered and the page ends up reloading. To solve this problem, I have set up a manual trigger for the form submission, ...

"Seeking clarification on submitting forms using JQuery - a straightforward query

My goal is to trigger a form submission when the page reloads. Here's what I have so far: $('form').submit(function() { $(window).unbind("beforeunload"); }); $(window).bind("beforeunload", function() { $('#disconnectform&apo ...

Simulate internationalization for vue using jest

Currently, I am working on setting up jest unit tests for a Vue project within a complex custom monorepo. I am facing an issue with i18n, which I use for translation management in my application. The problem arises with the following code snippet for init ...

Using JQuery for sending POST requests in JavaScript

I am currently facing an issue while trying to post JSON data to a server. I have managed to do it successfully using a certain method, which is as follows: <html> <head> <script src="http://code.jquery.com/jquery-latest.min.js">< ...

Describe the ng-model for this specific JSON input array in AngularJS

Aim: The task at hand is to update the data in the following format: "open_hours": [ // (Note that open_hours is an array). { "weekday": "mon", "opens_at": "09:00", "closes_at": "22:00" }, { "weekday": "t ...

"An error has occurred during the ANT build, resulting in java.lang.NoClass

I encountered an issue related to the classpath during the run phase. The error message reads as follows: run: [java] java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver [java] at java.lang.Class.getDeclaredMethods0(Native Method) [ ...

Tips for connecting a Django API project with a nodejs and react frontend

I'm currently working on a Django API project and I am considering incorporating Node.js into the mix. Additionally, I am interested in using React for the frontend of the application. Is this combination of technologies feasible? Would it be advisabl ...

Press one button to activate another button

I am looking to activate a button's class by clicking on another button. The button I have is: <button class="clear-cart banner-btn">clear cart</button> This button is used for clearing a cart. On the order confirmation page, I click on ...

Can a Unicode character be overwritten using a specific method?

Is there a way to display a number on top of the unicode character '♤' without using images? I have over 200 ♤ symbols each with a unique number, and using images would take up too much space. The characters will need to be different sizes, a ...

Puppeteer is unable to detect the node.js handlebars helpers

I'm utilizing puppeteer in NodeJs to generate a PDF file. I use handlebars to render the template and pass variables during compilation for handlebars to access them. Here is the current code snippet: const browser = await puppeteer.launch({ he ...

Can you explain the significance of this async JavaScript server application error?

While working on a weather app website connected to another site through a server, I encountered an issue with asynchronous JavaScript. Upon running the code, I received an error message stating "uncaught syntax error: unexpected end of input" in the last ...

Exploring the dynamic loading of components within routes

Just starting out with Vue and experimenting with vue-router, I'm trying my hand at dynamically loading components without relying on additional libraries like webpack. So far, I've set up an index page and a router. Upon initial page load, I not ...

The version of selenium-java-2.43.0 is not compatible with Firefox 36

After successfully running my tests with "selenium-java-2.43.0" on Firefox 33, I encountered an issue when Firefox updated to version 36. The web browser was unable to load the page. Should I consider upgrading to selenium-java-2.4x.0? ...

What is the best way to pass route props using the <router-link> component?

Check out the button below that links to a specific route: <router-link class="q-pa-md" :to="{ name: 'Edit'}" id="item.id"> <q-btn outline>Edit</q-btn> </router-link> Take a look at my router se ...