在Selenium中获取下拉菜单的定位器时出现问题

vqlkdk9b  于 2022-12-23  发布在  其他
关注(0)|答案(2)|浏览(167)

请查看页面@https://www.kayak.co.in/stays。当我在“位置”文本框中键入位置(例如新德里)时,将显示一个下拉列表,指示要选择的选项列表。但是,当我尝试使用开发人员工具通过单击该元素来标识该元素时,它消失了,并显示“我无法获取定位器”。我尝试将定位器与键操作Enter沿着传递:例如,SendKeys(locator + Keys.Enter)按照用户界面的行为,但即使它无法工作,这是阻止我的脚本。请让我知道如何继续。

如前所述,我不能得到下拉定位器和关键。进入甚至不工作

7gcisfzg

7gcisfzg1#

下面是一个完整的SeleniumBase脚本(https://github.com/seleniumbase/SeleniumBase),它可以帮你完成所有这些工作。pip install seleniumbase,然后用pythonpytest运行脚本。休眠只是为了让你能看到发生了什么。否则它会运行得很快!

from seleniumbase import BaseCase

if __name__ == "__main__":
    from pytest import main
    main([__file__])

class KayakTest(BaseCase):
    def test_kayak_search(self):
        self.open("https://www.kayak.co.in/stays")
        self.type(
            'input[placeholder*="Enter a city"]',
            "New Delhi, National Capital Territory of India"
        )
        self.sleep(1)
        self.add_text('input[placeholder*="Enter a city"]', "\n")
        self.sleep(1)
        self.click("div.ATGJ-monthWrapper div:nth-of-type(2) div:nth-of-type(2) div:nth-of-type(23)")
        self.sleep(1)
        self.click("div.ATGJ-monthWrapper div:nth-of-type(2) div:nth-of-type(2) div:nth-of-type(27)")
        self.sleep(1)
        self.click("svg.c8LPF-icon")
        self.sleep(5)
hrirmatl

hrirmatl2#

这是完整的XPath。请随意删除无用的部分。

//body[@id='k4-r'][@class='keel kl kl-override HotelsSearch react react-st wide a11y-focus-outlines wide-fd en_IN horizon'][@style='display: block;']/div[@id='RaKq'][@class='Common-Page-StandardPage Base-Search-LandingPage Base-Search-SearchPage Hotels-Search-HotelSearchPage cur_inr vm-fd dual-view']/div[@id='e6vK'][@class='Common-Layout-StandardBody withFooter']/main[@id='e6vK-pageContent'][@class='pageContent withDrawer moved wide ']/div[@id='RaKq-fd'][@class='SearchPage__FrontDoor']/div[@id='dbfN'][@class='Base-Frontdoor-FrontDoor Hotels-Frontdoor-HotelFrontDoor']/div[@id='VQMg'][@class='Common-Frontdoor-HarmonizedFrontDoorContent']/div[@class='coverPhotoContainer splash'][@data-search-form-container]/div[@id='x0Lx'][@class='_kkL _jcQ _ia2']/div[@id='dbfN-primary'][@class='primary-content']/section/div[@class='keel-container s-t-bp']/div[@class='form-section'][@id='main-search-form']/div[@class='form-container']/div[@id='c2Y_4'][@class='Ui-Searchforms-Hotels-Components-HotelSearchForm-container ']/div[@class='HPw7 HPw7-pres-default HPw7-pres-responsive HPw7-pres-dark HPw7-pres-rooms-guests HPw7-pres-wide-dates']/div[@class='HPw7-form-fields-and-submit']/div[@class='HPw7-form-fields']/div[@class='HPw7-destination']/div[@class='BCcW']/div[@class='k_my k_my-mod-theme-mcfly-search k_my-mod-radius-base k_my-mod-size-large k_my-mod-font-size-base k_my-mod-spacing-default k_my-mod-text-overflow-ellipsis k_my-mod-state-default']/input[@type='text'][@value][@class='k_my-input'][@tabindex='0'][@placeholder='Enter a city, hotel, airport, address or landmark'][@aria-autocomplete='list'][@aria-haspopup='listbox']

https://github.com/sputnick-dev/retrieveCssOrXpathSelectorFromTextOrNode生成
简化:

//input[@type='text'][@value][@placeholder='Enter a city, hotel, airport, address or landmark'][@aria-haspopup='listbox']

或在键入new delhi之后:

//input[@type='text'][@value='Near Indira Gandhi Airport New Delhi India, New Delhi, National Capital Territory of India, India'][@class='k_my-input'][@tabindex='0'][@placeholder='Enter a city, hotel, airport, address or landmark'][@aria-autocomplete='list'][@aria-haspopup='listbox'][@aria-activedescendant='1070140428_hotel_Near Indira Gandhi Airport New Delhi India, New Delhi, National Capital Territory of India, India']

相关问题