简体   繁体   English

Selenium Python中的元素识别问题

[英]Element identification problem in Selenium Python

Here is my current code:这是我当前的代码:

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

driver = webdriver.Chrome(executable_path= "C:\Drivers\chromedriver")
driver.get("https://www.expedia.com/")
driver.maximize_window()
driver.implicitly_wait(5)

driver.find_element(By.XPATH, "//*[@id='uitk-tabs-button-container']/li[2]/a/span").click()
time.sleep(15)
driver.find_element(By.ID, "location-field-leg1-origin-dialog-trigger").send_keys("BLR")
driver.find_element(By.ID, "location-field-leg1-destination-dialog-trigger").send_keys("BOM")
driver.find_element(By.ID, "departure").send_keys("Fri Nov 06 2020")
driver.find_element(By.XPATH, "//*[@id='root']/div/div[2]/div/div/div[2]/p/a").click()

I am trying to identify the elements我正在尝试识别元素

Flights(link), origin(inputbox), destination(inputbox), date(inputbox) and search(link)航班(链接)、起点(输入框)、目的地(输入框)、日期(输入框)和搜索(链接)

but no luck... always getting但没有运气......总是得到

NoSuchElementException: Message: no such element: Unable to locate element:... NoSuchElementException:消息:没有这样的元素:无法找到元素:...

I am a beginner to selenium, please help.我是selenium的初学者,请帮忙。

This is tricky because ultimately to set the departure and return dates you have to click on the calendar and select various date buttons.这很棘手,因为最终要设置出发和返回日期,您必须单击日历和 select 各种日期按钮。 Perhaps a more detailed investigation might reveal a way of executing a JavaScript script to add the dates to the underlying widgets.也许更详细的调查可能会揭示一种执行 JavaScript 脚本以将日期添加到底层小部件的方法。 But I will try to at least get you started.但我会尝试至少让你开始。 The main issue is that to enter, for example, a departure airport, you have to first click on the departure button, which then brings up a formerly hidden input area into which you can now enter your info (followed by a RETURN to close the input):主要问题是,例如,要进入出发机场,您必须先点击出发按钮,然后弹出一个以前隐藏的输入区域,您现在可以在其中输入您的信息(然后按 RETURN 关闭输入):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


driver = webdriver.Chrome(executable_path= "C:\Drivers\chromedriver")
driver.get("https://www.expedia.com/")
driver.maximize_window()
driver.implicitly_wait(5)
try:
    driver.find_element(By.XPATH, "//*[@id='uitk-tabs-button-container']/li[2]/a/span").click()

    # Leaving from:
    driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div/div[1]/div/div[1]/div[3]/div/figure/div[3]/div/div/div/div/div[2]/div/form/div[2]/div/div[1]/div/div[1]/div/div[1]/div/div/div/div/div[1]/button").click()
    driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div/div[1]/div/div[1]/div[3]/div/figure/div[3]/div/div/div/div/div[2]/div/form/div[2]/div/div[1]/div/div[1]/div/div[1]/div/div/div/div/div[2]/header/section/div/input").send_keys("BLR" + Keys.RETURN)

    # Arriving to:
    driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div/div[1]/div/div[1]/div[3]/div/figure/div[3]/div/div/div/div/div[2]/div/form/div[2]/div/div[1]/div/div[1]/div/div[2]/div/div/div/div/div[1]/button").click()
    driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div/div[1]/div/div[1]/div[3]/div/figure/div[3]/div/div/div/div/div[2]/div/form/div[2]/div/div[1]/div/div[1]/div/div[2]/div/div/div/div/div[2]/header/section/div/input").send_keys("BOM" + Keys.RETURN)

    # Departing:
    # open calendar:
    driver.find_element(By.ID, "d1-btn").click()
    input('pausing so you can see the calendar...')
    # pick dates (this is the tricky bit) -- code omitted:
    # finally click "Done":
    driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div/div[1]/div/div[1]/div[3]/div/figure/div[3]/div/div/div/div/div[2]/div/form/div[2]/div/div[1]/div/div[2]/div/div/div/div[1]/div/div[2]/div/div[2]/button").click()
finally:
    input('pausing before termination...')
    driver.quit()

Note that the browser window must have the focus (be the current window) or the find_element calls might fail.请注意,浏览器 window 必须具有焦点(成为当前窗口),否则find_element调用可能会失败。 Anyway, that has been my experience.无论如何,这是我的经验。

This is what I see:这是我看到的:

在此处输入图像描述

在此处输入图像描述

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

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