Populating a dropdown menu within a form by extracting the text contained within the <option> tags utilizing CasperJS

I am trying to complete the "Acheter un billet" form on this website:

This is my current progress:

var casper = require('casper').create();

casper.start('http://www.leguichet.fr/', function() {
  this.fill('form#search_tickets', {'departure':'1', 'arrival':'2'}, false);
  this.click('input[value="Rechercher"]');
  this.wait(1000, function() {
    this.echo(this.getCurrentUrl());
  });
});

casper.run(function(){
    this.exit();
});

The documentation mentions that fill() uses the value attribute for matching, but I prefer to use the text within the option tags. For example, they have:

<option value="Montpellier">Montpellier</option>
<option value="Béziers">Béziers</option>

So if I want to select Béziers, do I write 'departure':'Montpellier'? Is there a way to utilize the text within the option tags?

Answer №1

To simplify the process, you can retrieve the element value of the desired option and use it for selection later on. Even if another option is chosen during subsequent selections, the value remains the same so it should not have any impact:

var x = require('casper').selectXPath;
casper.start('http://www.leguichet.fr/', function() {

  var textToSelect = "Béziers";
  var value = this.getElementAttribute(x("//form[@id='search_tickets']//select[@name='departure']/option[contains(text(), '" + text + "')]"), 'value');

  this.fill('form#search_tickets', {'departure': value, 'arrival':'2'}, false);

  this.click('input[value="Rechercher"]');
  this.wait(1000, function() {
    this.echo(this.getCurrentUrl())
  });
});

You can utilize XPath to easily select DOM nodes based on their text content using the text() function. Apply the same approach for selecting arrival.

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

Extract the color value using the Spectrum color picker

I successfully integrated the Spectrum color picker into my project. $(document).ready(function() { $("#font_color").spectrum({ color: "#f00" }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></scri ...

Perform the ajax request repeatedly only once

I'm looking for a way to ensure that my ajax post request only gets executed once, even if the user refreshes the page multiple times. I considered using cookies but I'm not sure how to implement it. Can anyone help? Below is my jQuery code: va ...

Utilizing the Bootstrap portfolio section, I aim to eliminate the 'ALL' tab and ensure a category is selected

Currently, I am utilizing this template If you scroll down to the WORK section, you will find the portfolio section which is filterable. The default selected option is "ALL," displaying all items. However, I would like to remove this and activate a diffe ...

Be sure to transmit the value in string format when choosing the option

While working with react-select for autocomplete and option related fields, I encountered an issue. When selecting an option, the entire object is passed like {value: 'abc', label: 'ABC'}, but I only wanted to pass the value as a string ...

Facing issue with React Native Flip Card functionality not working properly when trying to flip

I am currently working on developing a flip card game using GestureFlipView for the flip card animation. My goal is to display these flip cards in a 3X3 grid by utilizing components from React Native. However, I have encountered an issue where the cards ar ...

The child div can be scrolled horizontally, while the parent window remains fixed

I am facing a challenge with my HTML and CSS setup. I want to create a child div that scrolls horizontally, but I do not want the parent window to scroll along with it. Below is my current code: <div id="parent"> <div id="div1"> . ...

"Implementing Babel in a create-react-app Project: A Step-by-Step

Currently, I am working on a React.js project that was created using create-react-app. In order to connect with the server using axios, I had to incorporate babel-polyfill to ensure compatibility with IE11. The issue is that create-react-app does not provi ...

Refresh needed for Material UI styles to load correctly in Next JS

UPDATE: Reproducible issue https://github.com/ganavol409/next-material-ui-classes-bug Issue seems to be related to Higher Order Components and importing useStyles from Material UI Implemented Solution: https://github.com/mui-org/material-ui/blob/master/ ...

Make changes to external CSS using HTML and JavaScript

Is it possible to dynamically change the value of a background in an external CSS file using JavaScript? Currently, all my pages are connected to a CSS file that controls the background for each page. I am attempting to modify the background by clicking a ...

Pass an array containing an HTML string to the $.post method in jQuery

I have a problem with displaying HTML content fetched from a PHP script using the jQuery $.post method: $.post('{$site_url}tests/tests/content', params, function (data) { $('#some-div1').html(data[1]); $('#some-div2').htm ...

Guide on how to reset a counter to 0 using a JavaScript button

I currently have a JavaScript counter and a button that increments the counter by 1. Here is my existing code: var counter = 0; var a = 0; var add = function(valueToAdd){ a += valueToAdd; document.getElementById('Value&ap ...

Toggle the input box by clicking the button

How do I show or hide the input box (blue square) when I click the search button (red square)? Link Image I attempted to create the transition in CSS and also experimented with JavaScript, but the JavaScript part confused me. Here is what I tried: $(" ...

Executing a JavaScript function within HTML on the server side using Node.js

Recently, I've been working with an index.html file that references a CircleArea.js script. The HTML code looks like this: <!DOCTYPE html> <html> <head> <title>Server-side Example</title> <script sr ...

Tips for exiting a web app that is taking up your entire screen

We're currently working on making our web app run in full screen mode. We were able to achieve this by implementing these meta tags: <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar ...

Is it possible to retrieve an array using axios?

Currently, I am exploring computed properties in Vue.js. One of the computed methods I am working on involves making a request to an axios API to retrieve an array after applying some logic within the promise. computed: { filteredTrips: function () { ...

Is it more efficient to pass session variables through an ajax function, or should I access them directly within the script?

I am currently working on a page that utilizes an ajax function to retrieve updates from another page. The function requires the user's id, which is obtained from a session variable, to fetch any new updates. These updates are then displayed in a spec ...

Implementing personalized validation post-control initialization in an Angular 4 reactive form

I have been working on an Angular 4 application and using reactive forms. For one of my forms, I am trying to implement custom validation on a control after it has been initialized, based on whether a checkbox is checked or unchecked. CODE form.componen ...

CellNav + Edit feature results in the grid taking over focus from elements located outside of the grid

I am currently dealing with an issue in my application where I am using ui-grid along with cellNav and setting editOnFocus=true for certain columns. The problem arises when the END_CELL_EDIT and CANCEL_CELL_EDIT events in the edit feature always trigger gr ...

Add a fresh AngularJS controller to the HTML dynamically once the page has finished loading

I am currently developing an AngularJS website that fetches data from a web service/API. One API returns HTML and AngularJS code in order to dynamically add controllers or other new Angular components as needed. This specific string will be included in the ...

"Bridging the Gap: Establishing Communication Among Backbone Marionette Views

I recently started working as a junior javascript developer at a company that utilizes Backbone and Marionette. My first project involves implementing searching, filtering, and sorting capabilities in a collection based on user input. The challenge lies in ...