WebDriver and Python: A guide on capturing a dynamically changing URL and storing it in a variable efficiently

Python and Selenium webdriver script:


        elem = driver.find_element_by_css_selector("#username")
        elem.send_keys("username")
        elem = driver.find_element_by_css_selector("#password")
        elem.send_keys("password")

        driver.implicitly_wait(2) #seconds
        elem = driver.find_element_by_css_selector("button")
        elem.click()

        def condition(driver):
            look_for = ("url_which_contains_this_text")
            url = driver.current_url
            for look_for in url:
                if url.find(look_for) != -1:
                    print url

            return url

        #page_url = driver.current_url
        print url
    

This code performs the following tasks: 1) User logs in; 2) Clicks 'login' button. After that, it needs to capture the dynamically changing URL which may involve an access token being loaded, etc. The goal is to identify and save the URL that contains the value "example_id=" into a variable and print it.

Can anyone assist with this?

Answer №1

I'm not entirely certain if this method will be successful since I am unable to conduct a test; however, you could potentially attempt the following approach:

targeted_url = ""
while True:
    current_address = driver.current_url
    if "sample_id=" in current_address:
        targeted_url = current_address
        print('\n'+targeted_url)
        break
    else: print(current_address)

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

Searching for a solution on interacting with elements within the Shadow DOM using Selenium in Python?

Apologies for the lack of details, allow me to provide a comprehensive explanation. Situation: I aim to click on the 3-dot option menu, then select "Export" from the popup sub-menu and choose "CSV" in the subsequent pop-up sub-menu. Issue at Hand: The 3- ...

Having trouble invoking the deserialize function accurately; struggling to locate the relevant method or its invocation location

One model I'm working with is: class CdrSheet(models.Model): MONTH_CHOICES = ((1, "January"), (2, "February"), (3, "March"), (4, "April"), (5, "May"), (6, "June"), (7, "July"), (8, "August"), (9, "September"), (10, "October") ...

Retrieve the browser version through the use of the Selenium WebDriver

Is there a way to retrieve the browser version? >>> from selenium import webdriver >>> driver = webdriver.Chrome() >>> print(version) <-- Any suggestions on how to accomplish this? Chrome 92.0 ...

Encountering an error while attempting to update the selenium version: [debug] [MJSONWP] Bad parameters: BadParameters

Everything was running smoothly with the Automation suit until the selenium upgrade to version 3.9.1. After the update, I encountered the following error while initializing the Android driver. [HTTP] --> POST /wd/hub/session {"desiredCapabilities":{"ap ...

Creating fake classes and mocking class methods in Python unit tests

Using python's unittest.mock for testing in a Django application, I am trying to verify that a class is called and a method on its instance is also called. Consider the following simplified example code: # Inside project/app.py def do_something(): ...

Error encountered while attempting to parse schema in JSON format in PySpark: Unknown token 'ArrayType' found, expected JSON String, Number, Array, Object, or token

Following up on a previous question thread, I want to thank @abiratis for the helpful response. We are now working on implementing the solution in our glue jobs; however, we do not have a static schema defined. To address this, we have added a new column c ...

Selenium: forever loading dynamic content non-stop

I am attempting to download multiple annual accounts in PDF format from a freely available database in Belgium. My plan is to utilize selenium for this task. However, I'm facing an issue where the dynamic content keeps loading endlessly after enteri ...

Convert pandas column containing string representation of a list to multiple rows (resolving literal_eval issue)

Dealing with a dataframe that contains a column of lists stored as strings, each with varying sizes. The goal is to split the items within the lists into multiple rows. df['urls'] 0 '[http://sth/sth]' 1 '[http://sth.COM, htt ...

Retrieving a JavaScript variable's value in Selenium with Python

rwdata = driver.find_elements_by_xpath( "/html/body/div[2]/div/div/div/form[2]/div[2]/table/tbody") This table contains the value of variable r as text/javascript type. for r in rwdata: print(r.text) if r.text != "Booked": ...

The if condition in Kantu is throwing an error due to an invalid token being

Recently, I decided to give the Kantu web automation tool a try for the first time. While most of it seems straightforward, I've come across an issue when trying to loop through a CSV file. Here's the relevant section of my script: { "Comm ...

Guide on transitioning an App from the Foreground to the Background and back using Selendroid

Can you provide guidance on putting an App in the background and bringing it back to the foreground using Selendroid? I would greatly appreciate your advice. ...

What is the reason behind object addresses always being represented by 8 hex digits?

Illustrations: 0xb6f99cec 0xb6f99d2c 0xb6f99d6c 0xb6f99dac What is the rationale behind choosing 8 for this format? Is it a deliberate design choice, a memory optimization, or does it serve a specific purpose compared to other values like 4, 12, or 16 ...

Error: Unable to locate the module titled 'bs4'. The module cannot be utilized at this time

Hello everyone! I'm a beginner in Python and currently using Python 3.6.4 (64-bit). I recently installed pandas and matplotlib successfully, but I'm facing difficulties importing bs4. Can someone please provide guidance on how to resolve this is ...

What is the best way to execute a Druid SQL query with the "timeseries" function in pydruid?

I have written the following code in Druid SQL and my objective is to execute it from Python. While I am currently able to do so using DB API, I am curious if there is a way to achieve this using the hydroid function "timeseries" as it would integrate bett ...

Using Selenium WebDriver version 2.32.1 in C#, ensure that you wait for the loading div to be hidden after the page has finished loading

Apologies if this question has already been addressed, but I was unable to locate it. Please forgive my lack of knowledge as I am still new to WebDriver. Upon the initial page load, a LOADING DIV is displayed until all data is loaded. How can I ensure th ...

I am having trouble running a Python script within an Express application

I have a Python script that turns on an LED on my Raspberry Pi, and I want to execute it when a button is clicked on my Express app. I've tested the script and it works fine, but for some reason, when I try to run it from the server, it doesn't w ...

What methods can we use to extract Instagram follower information from an influencer's profile, utilizing either API or selenium?

I have been exploring the features of the Instagram web browser, and discovered that it allows you to access the list of followers for any public user. By clicking on the followers URL, a new window opens that paginates through the viewers. As this feature ...

Obtaining solely the words found within HTML documents

In my Python 2.7 project, I have a folder containing multiple HTML pages that I need to extract only words from. My current process involves opening the HTML file, using the Beautiful Soup library to extract text, and then writing it to a new file. However ...

What is the process for converting text data to a variable in Python?

update: I am looking to utilize specific variables extracted from a text file as input for my code. These variables need to be stored as individual entities. I currently have 2 python scripts. The first script retrieves data from a .txt file with contents ...

Access several web drivers simultaneously without the need to log in each time

Currently, I am attempting to execute selenium utilizing ThreadsPoolExecutor. The particular website I'm working on necessitates a login process, which is causing some delays in the automated tasks that I want to perform on the site. Each time a threa ...