【问题标题】:Python Selenium: Unable to Locate Element on AmazonPython Selenium:无法在亚马逊上找到元素
【发布时间】:2021-03-30 03:44:29
【问题描述】:

我一直在尝试填写输入:

<input id="turbo-checkout-pyo-button" data-testid="" class="a-button-input" type="submit" value="Place your order" aria-labelledby="turbo-checkout-place-order-button-announce">

我试过了:

placeorderBtn = order = browser.find_element_by_id("turbo-checkout-pyo-button")

我总是得到:

无法定位元素:{"method":"css selector","selector":"[id="turbo-checkout-pyo-button"]"}

它似乎不起作用。我不知道我是否提供了足够的信息,所以如果您需要更多信息来帮助我,请告诉我!

【问题讨论】:

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

您为什么要使用 = 两次? 只需使用:

placeorderBtn = browser.find_element_by_id("turbo-checkout-pyo-button")

另外,请等待此按钮变为可点击状态。 消息说您正在使用 css 选择器。 正确使用它。 # 为 ID 元素添加。 在您的情况下,它将是:

placeorderBtn = browser.find_element_by_css_selector("#turbo-checkout-pyo-button")
placeorderBtn.click()

尝试 2: 基于 cmets: 试试这个 xpath:

locator = "//input[text() = 'Place your order']"

或者,

locator = "//span[text() = 'Place your order']"

尝试 3:

点击使用

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable(
                (By.XPATH, "//span[text() = 'Place your order']")))
locator = driver.find_element_by_xpath('//span[text() = 'Place your order']')
locator.click()

尝试 4:

使用定位器:

placeorderBtn = browser.find_element_by_id("turbo-checkout-place-order-button-announce") 
placeorderBtn.click()

【讨论】:

  • 仍然给我:无法定位元素:{"method":"css selector","selector":"#turbo-checkout-pyo-button"}
  • 页面加载完毕了吗?
  • 用于调试添加 time.sleep(10),其中 10 是等待的秒数。它可以更少。如果可行,请添加显式等待(检查我的其他答案热门)
  • 另外,我不能投票,因为我没有足够的声誉,大声笑,但我绝对会在查看链接后接受您的回答!再次感谢您!
  • 没关系!我想通了!非常感谢您的时间和帮助!这对我很重要。虽然我对此并不陌生,但我已经花了几个小时在这上面,而你主要帮助了我!再次感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 2016-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多