1

I want python script to be triggered by entering specific URLs with specific part of address.

Here is an example:

http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1234
#1234 can be any random 4 digits Number

Basically I want python script to activate when URL has "http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=" in it.

Here is what I wrote so far:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome('./chromedriver')
wait = WebDriverWait(driver, 9999)
desired_url = http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=\d{4}
def wait_for_correct_current_url):
      wait.until(lambda driver: driver.current_url == desired_url)



driver.get("http://www.google.com")
wait_for_correct_current_url(desired_url)
**(Script that activates after entering desired_url)**

I am wondering if regex will do the trick, but I am new to python... so what do i know.

Thanks in advance!

2
  • Use this selenium api expected_conditions.url_changes Commented Feb 21, 2019 at 5:33
  • I got this: 'WebDriver' object has no attribute 'support' Commented Feb 21, 2019 at 5:45

3 Answers 3

1

Use like this,

from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 1000).until(EC.url_contains("desired_url"))
Sign up to request clarification or add additional context in comments.

Comments

0

A bit more information about your usecase would have helped to provide a more canonical answer.

However to invoke a URL with specific part of address to be random, you can identify the range through range() and iterate the range through format() to construct the complete URL and you can use the following solution:

  • Code Block:

    for i in range(1000,1010):
        desired_url = "http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER={}".format(i)
        print(desired_url)
    
  • Console Output:

    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1000
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1001
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1002
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1003
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1004
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1005
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1006
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1007
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1008
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1009
    

Incase you are looking for random numbers as a part of the URL, you can use randrange() to generate as follows:

  • Code Block:

    import random
    
    for i in range(0,10) :
        desired_url = "http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER={}".format(str(random.randrange(1000, 9999)))
        print(desired_url)
    
  • Console Output:

    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=8776
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1662
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=3255
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1524
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=6463
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=4511
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=3273
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=7471
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=4672
    http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=2828
    

Comments

0

Tried all answers above with no luck. Thanks anyways. I guess my explanation was quite not enough

This did a trick for me.

def condition(driver):
look_for = ("Any word within expected URL1", "Any word within expected URL2")
url = driver.current_url
for s in look_for:
    if url.find(s) != -1:
        [Any Script I want to activate]

return False

wait.until(condition)

1 Comment

How does this solution answers the question where you mentioned ...script to be triggered by entering specific URLs with specific part of address... and you were looking for ...I am wondering if regex will do the trick, but I am new to python... so what do i know...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.