Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return to previous page after driver.get #2018

Open
hkpmatt opened this issue Sep 13, 2024 · 0 comments
Open

Return to previous page after driver.get #2018

hkpmatt opened this issue Sep 13, 2024 · 0 comments

Comments

@hkpmatt
Copy link

hkpmatt commented Sep 13, 2024

I try to automate a court booking procedure and when I have automated the login action.
I try to navigate the driver to a new site that will help me to go to the booking site.
It can get to the desired URL if there is no more action after driver.get(URL) and yet,
When I add action to click the button after driver.get(URL) the script will return to the login page after driver.get(URL) and the address bar poped up the URL and right back to login page.

`from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
#from selenium.webdriver.edge.options import Options
import undetected_chromedriver as uc
from undetected_chromedriver import Chrome, ChromeOptions
from selenium.webdriver.support import expected_conditions as EC

import os
from dotenv import load_dotenv
import time
load_dotenv()
username = os.getenv('UNAME')
password = os.getenv('PASSWORD')
venue = "venue"
bookdate = '2024-09-12'
booktime = '2000'
sport = "basketball"
#print(username,password)
options = ChromeOptions()
options.page_load_strategy = 'none'
options.add_argument('--disable-notifications')
options.add_argument('--incognito')
options.add_argument("--start-maximized")

#options.add_experimental_option("detach", True)

driver = Chrome(use_subprocess=True, options=options)
def span_click(driver, xpath):
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, xpath))).click()

def span_keys(driver, xpath, value):
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, xpath))).send_keys(value)

driver.get("https://www.bookingsite.com")

#click login
span_click(driver,"//a[@Class='btn btn--primary btn--grad btn--grad-org-outline']")

time.sleep(2)

#new tab after clicking login button
driver.switch_to.window(driver.window_handles[1])

#enter username, password and click login button
span_keys(driver,"//input[@name='pc-login-username']",username)
span_keys(driver,"//input[@name='pc-login-password']",password)
span_click(driver,"//div[@name='pc-login-btn']")
#time.sleep(1)

#try to wait for element appear after logged in
try:
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//ul[@Class='el-menu-demo el-menu--horizontal el-menu']")))
except Exception as e:
print(f"An error occurred: {e}")

#get to the booking detail page
search_url = f"https://www.bookingsite.com/facilities/search-result?keywords={venue}&district=&startDate={bookdate}&typeCode=BASC&venueCode=291&sportCode=BAGM&typeName={sport}&frmFilterType=&venueSportCode=BAGM&isFree=false"

driver.get(search_url)

#When I add the below step, the browser cannot get to the booking detail page, but it can if i remove the belows
span_click(driver,"//div[@Class='facility-search-box']//div[@Class='text' and contains(span, 'search sport、venue')]")
span_keys(driver,"//div[@Class='search-associate-input-box']//input",venue)`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant