简体   繁体   English

如何使用 selenium 导航到下一页?

[英]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.所以我在登录后尝试导航到 web 页面,当我执行代码时,它登录但从不导航到下一页。 I have tried navigating to the next page in the same function, and in seperate, but it doesn't move onto the next one.我曾尝试单独导航到同一 function 中的下一页,但它没有移动到下一页。 On MacOS Big Sur using Pycharm, python 3.9.在 MacOS Big Sur 上使用 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.以下是您如何导航到该项目(减去所有登录内容),而无需使用多个get调用。

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.对于send_keys ,您可能也可以使用较短的搜索词,您必须尝试一下。

Basically it will:基本上它会:

  1. Go to Amazon.com Go 至亚马逊。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".我选择了第一个不是“赞助”的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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