Obtaining a particular ID from a URL using PHP WebDriver Selenium: A Comprehensive Guide

I'm trying to figure out how to extract a specific ID from a URL. (Is this even possible?)

For instance:

// Current URL: http://test.be/certificate/create
// Saving new certificate 
$search11 = $this->webDriver->findElement(WebDriverBy::id('certificate_save'));
$search11->click();

// Current URL: http://test.be/certificate/11/basicinfo
// I need to extract the ID '11' to proceed to the next page

// Current URL: http://test.be/certificate/11/holders

I simply want to capture the number that comes after /certificate/ Any ideas or suggestions? Feel free to ask if anything is unclear.

Thank you in advance

Best regards

Answer №1

To retrieve the URL from the Driver object and extract the ID, you can use the following code snippet:

String url = driver.getCurrentUrl();
String[] parts = url.split("/");
int id = Integer.parseInt(parts[3]);

NOTE: I see that you are working with PHP, but the same concept can be applied to PHP as well.

$url = $driver->getCurrentURL();
$parts = explode('/', $url);
$id = $parts[4];

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

Xpath merging HTML elements or nodes

<tr id="computer-report-tbl-row-0" ng-repeat="item in table.data.items" class="ng-scope" style=""> <td id="column-name-0" class="table-cell-md" sc-ellipsis-title="" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"> ...

Instructions on how to configure an extension in geckodriver using Selenium with Python

Recently, I attempted to load the Zenmate VPN extension in the Firefox driver using the following code. Despite successfully opening the driver, the extension failed to load. from selenium import webdriver profile = webdriver.FirefoxProfile() profile.add_ ...

I am facing issues with opening my web driver sampler for jmeter

My JMeter Webdriver sampler is encountering an issue while trying to open. The error message I received is as follows: 2019-07-09 18:55:00,153 ERROR o.a.j.JMeter: Uncaught exception: java.lang.NoSuchMethodError: com.google.common.base.Preconditions.che ...

The driver.quit function in Selenium version 3.0.2 with Firefox version 50.1.0 is currently experiencing a malfunction

I've been working with Selenium 3 and interacting with Firefox version 50.1.0 When I try to use driver.quit() to close the browser, Firefox gives an error. driver.close() doesn't work either. Could this be a version compatibility issue? If so, ...

Exploring the power of protractor through the implementation of loops

Within my Protractor loop, the loop index (i) is not behaving as expected. Issues: Error: Index out of bound. Attempting to access element at index:'x', but there are only 'x' elements or The index remains static and always equ ...

Unable to select dropdown button in Python while using Selenium

My goal is to click on the "Project" element in order to display a dropdown list (as shown in the image below). When using the selenium library in Python, I encountered the following error: could not be scrolled into view This error was triggered by code ...

Navigating to an iframe window within a single window on a Windows application

Initially, I attempted to use driver.switchTo().frame("framename"); but unfortunately it did not successfully switch to the desired frame. driver.switchTo().frame("xxx");/// Throws an {"status":7,"value":{"error ...

Is it possible to download a single file into multiple folders using Webdriver in Java?

My objective is to download a file in 4 different locations using the code below. Currently, the file is being saved in the default Downloads folder. System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/src/Brows ...

An issue with Selenium web scraping arises: WebDriverException occurs after the initial iteration of the loop

Currently, I am executing a Selenium web-scraping loop on Chrome using MacOS arm64. The goal is to iterate through a list of keywords as inputs in an input box, search for each one, and retrieve the text of an attribute from the output. A few months ago, I ...

Error encountered with Selenium due to NoSuchDriverError in Internet Explorer caused by VBA with

Encountering an issue while trying to launch IE using Selenium Web Driver. The error message states that Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (NoSuchDriver). https://i.stack.imgur.com/hzNLg.png Focusi ...

Encountering a null sessionId exception with Selenium RC while attempting to activate the JQuery AddLocationStrategy feature

I've been struggling all day to activate JQuery locators in Selenium RC by trying various suggestions found online, but unfortunately, without any success. I followed the recommendations from this thread on enabling JQuery locators: How do I add a JQ ...

Clearing Cookies and Cache in the Google Chrome Browser

Can you verify if I am able to utilize the functionality below for deleting cookies in Chrome: driver.manage().deleteAllCookies() Additionally, could you please provide the instructions for clearing the cache in both Chrome and IE? And how can one confir ...

Selenium causes the Chrome browser to instantly shut down upon opening

I am having an issue with my basic Python script that is supposed to open a Chrome window. However, when I run the code, the window appears briefly and then immediately closes. from selenium import webdriver import time browser = webdriver.Chrome(executab ...

Press the button using Python's Selenium

I am encountering an issue where I am unable to click the button below and receiving an error message indicating that it cannot be found. <div class="sportsbook-outcome-cell__body selected" aria-label="Thomas Almeida " data-tracking= ...

Fixing issues with Selenium and Chromedriver on AWS Lambda (unable to access chrome)

I have successfully deployed a Lambda Function for headless chrome with Python Selenium using the Serverless framework. However, while the function runs fine locally, it crashes on Lambda. Key details: (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c1 ...

Can someone please help me with automating the task of using Python to click on a button, download an Excel file, and then save it into a

Currently, I am working on a personal project that involves web scraping financial data and scheduling it to run daily using Windows Task Scheduler. This is the code snippet I'm using: import requests from selenium import webdriver from selenium.webd ...

What is the most efficient way to initialize the String originalHandle as driver.getWindowHandle()?

I have been working with TestNG and Selenium lately. I am attempting to utilize driver.getWindowHandle(); to navigate between pop-ups, iframes, and other elements. However, when I define it like this in the TNGDriver class public String originalHandle = ...

The Java for loop consistently evaluates to true

I encountered an issue with the following code. Despite having two failed scenarios, the result always shows as pass. List<WebElement> elementsList = driver.findElements(By.className("android.widget.TextView")); for (WebElement element : elementsL ...

Error in before_scenario: WebDriverException - Authorization needed

Greetings everyone! I am currently working on running some appium tests on browserstack using behave with python. However, I have encountered a particular issue that I need help with. Here is the project structure: - data -- credentials.yaml -- param ...

How do I properly send the Enter key in the Messages app using Appium on Mac OS?

Currently, I am working on a framework that utilizes Appium for Mac to test native MacOS apps. One issue I have encountered is the difficulty in executing a simple command to press the Enter key in the Messages app in order to send a message. To simulate t ...