Home > Back-end >  Locating the Submit Button URL with Selenium Python
Locating the Submit Button URL with Selenium Python

Time:09-17

I'm trying to get to the URL behind the submit button:

HTML

 from selenium import webdriver

browser = webdriver.Safari(executable_path = '/usr/bin/safaridriver') 

#Fill in the required field
default_input = '111'
browser.get('https://trackapkg.com/aramex-tracking-number')
field = browser.find_element_by_xpath('//*[@id="ShipmentNumber"]')
field.send_keys(default_input)

url = browser.current_url 

#Click submit button to get the new URL
browser.find_element_by_xpath("//span[@class='input-group-btn']").click();
while url == browser.current_url:
    time.sleep(5)
url = browser.current_url
print(url)

Submitting works (though inconsistently), but apparently there's an issue with Xpath to the button itself so it's not clicked and the URL can't be caught. I've tried multiple variants:

browser.find_element_by_xpath("//*[@id='ShipmentNumber']/input[@class='btn btn-success']").click()

Or this:

browser.find_element_by_xpath("//input[@class='btn btn-success']").click()

But still can find the solution. I will appreciate your advice

CodePudding user response:

The fault might be not waiting until the element is clickable. You could use

from selenium.webdriver.support.ui import WebDriverWait

submit = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//input[@class='btn btn-success']"))) 
submit.click()

CodePudding user response:

If clicks on the button is intermittent on safari browser. Then here is one workaround. You can use enter key to click on the search box.

default_input = '111'
browser.get('https://trackapkg.com/aramex-tracking-number')
field = browser.find_element_by_xpath('//*[@id="ShipmentNumber"]')
field.send_keys(default_input)
field.send_keys(Keys.ENTER)

You need to import following library.

from selenium.webdriver.common.keys import Keys

Therefore your code block would be like this

default_input = '111'
browser.get('https://trackapkg.com/aramex-tracking-number')
field = browser.find_element_by_xpath('//*[@id="ShipmentNumber"]')
field.send_keys(default_input)

url = browser.current_url 

field.send_keys(Keys.ENTER)# press enter key for search

time.sleep(1) # wait for 1 second to check
while url == browser.current_url:
    time.sleep(5)
url = browser.current_url
print(url)

CodePudding user response:

Things to be noted down.

  1. Use Explicit waits for more stability.

  2. Use relative xpath, prefer css over xpath. Also I see the id and 'Name' is unique so use id and 'Name' rather than xpath. Please see illustration below.

  3. Launch browser in full screen mode.

  4. When we click on GO, it opens a new tab, in Selenium we have to switch to new tab first before accessing anything in the new tab.

Sample code:

from selenium import webdriver

browser = webdriver.Safari(executable_path = '/usr/bin/safaridriver')

browser.maximize_window()
browser.implicitly_wait(50)
wait = WebDriverWait(browser, 20)
driver.get("https://trackapkg.com/aramex-tracking-number")
default_input = '111'

field = wait.until(EC.visibility_of_element_located((By.ID, "ShipmentNumber")))
field.send_keys(default_input)


url = browser.current_url
print('current url', url)

#Click submit button to get the new URL
wait.until(EC.element_to_be_clickable((By.NAME, "track"))).click()
time.sleep(5)

#When we click on GO, it opens a new tab, in Selenium we have to switch to new tab first before accessing anything in the new tab.

all_handles = browser.window_handles
browser.switch_to.window(all_handles[1])
new_url = browser.current_url
print('new url', new_url)

Output :

current url https://trackapkg.com/aramex-tracking-number
new url https://www.aramex.com/in/en/track/shipment-details?q=c2hpcG1lbnRJZD0xMTEmcHJvZHVjdEdyb3VwPUZSVCZzaGlwbWVudFR5cGU9Y2FyZ293aXNlJnNlYXJjaE1vZGU9YnlJZCY=-nc4VTy84z6w=
  • Related