简体   繁体   中英

How to navigate to next page using selenium?

So im trying to navigate to a web page after signing in, and when i execute the code, it signs in but never navigates to the next page. I have tried navigating to the next page in the same function, and in seperate, but it doesn't move onto the next one. On MacOS Big Sur using Pycharm, python 3.9.

*I left out the authentication code, but cannot get the driver to navigate to a page after the sign in page.

driver.get('https://www.amazon.com/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2F%3Fref_%3Dnav_signin&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=usflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&')
driver.get('https://www.amazon.com/gp/product/B071FXZBMV/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1')
def amazon_login_buy():
    driver.get('https://www.amazon.com/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2F%3Fref_%3Dnav_signin&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=usflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&')
    # enter email here
    driver.find_element_by_id('ap_email').send_keys('')
    driver.find_element_by_id('continue').click()
    # enter password here
    driver.find_element_by_id('ap_password').send_keys('')
    driver.find_element_by_name('rememberMe').click()
    driver.find_element_by_id('signInSubmit').click()


def amazon_navigate():
    # driver.implicitly_wait(2)
    driver.get('https://www.amazon.com/gp/product/B071FXZBMV/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1')


amazon_login_buy()

amazon_navigate()

Here is how you can navigate to that item (minus all the log in stuff), without using multiple get calls.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

def amazon_login_buy():

    wait = WebDriverWait(driver,10)

    driver.get('https://www.amazon.com')
    textbox = driver.find_element_by_id("twotabsearchtextbox")
    textbox.send_keys("OHill Cable Clips,16 Pack Black Adhesive Cord Holders, Ideal Cable Cords Management for Organizing Cable Wires-Home, Office, Car, Desk Nightstand")
    textbox.send_keys(Keys.ENTER)
    item = wait.until(EC.element_to_be_clickable((By.XPATH,"(//div[@data-component-type='s-search-result'])[3]//img")))
    item.click()



amazon_login_buy()

You could probably get away with a shorter search word as well for send_keys , you'd have to play around with that.

Basically it will:

  1. Go to Amazon.com
  2. Search for the item you want in the search box
  3. Click the image of the item from the resulting list of items. I was guessing which one you actually wanted, you can experiment with that piece. I chose the 1st one down that wasn't "sponsored".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM