Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 找不到自动完成选项的元素_Python_Selenium_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Python 找不到自动完成选项的元素

Python 找不到自动完成选项的元素,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,在我的应用程序中,我有一个文本框,当我输入城市名称时,它会将城市名称带到 选择一个城市。当我试图定位该元素时,它表示无法定位该元素 这是HTML代码 <ul id="autocomplete-options-fd419e03-fc56-725a-dc68-0dd3e55a2b19" class="autocomplete-content dropdown-content" tabindex="0" style="display: block; width: 231.948px; left:

在我的应用程序中,我有一个文本框,当我输入城市名称时,它会将城市名称带到 选择一个城市。当我试图定位该元素时,它表示无法定位该元素

这是HTML代码

<ul id="autocomplete-options-fd419e03-fc56-725a-dc68-0dd3e55a2b19" class="autocomplete-content
dropdown-content" tabindex="0" style="display: block; width: 231.948px; left: 5px; top: 54px; 
height: 50px; transform-origin: 0px 0px; opacity: 1; transform: scaleX(1) scaleY(1);">
<li class="active"><span><span class="highlight">Hyderabad</span></span></li></ul>
我得到以下错误

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate 
element: {"method":"css selector","selector":"[id="autocomplete-options-fd419e03-fc56-725a-dc68-0dd3e55a2b19"]"}
  (Session info: chrome=76.0.3809.132)

我认为您的问题在于以下代码行:

html_list=driver.find_element_by_id("autocomplete-options-fd419e03-fc56-725a-dc68-0dd3e55a2b19")
items = html_list.find_elements_by_tag_name("li")
ID
autocomplete-options-fd419e03-fc56-725a-dc68-0dd3e55a2b19
看起来像一个随机化的ID,每次刷新页面时都会更改。在这种情况下,您希望使用更健壮的选择器

我认为根本不需要获得
项目
列表。我认为您可以用一行代码替换上面的两行代码:

driver.find_element_by_xpath("//span[text()='Hyderabad']")
这将找到文本为海得拉巴的
span
元素

现在,您的代码将如下所示:

driver.find_element_by_name('residential_city_id').send_keys("Hyderabad")
tme.sleep(5)
driver.find_element_by_xpath("//span[text()='Hyderabad']")
要在文本为海得拉巴的自动完成元素上单击()
,您需要引导WebDriverWait使
元素可单击()
,并且您可以使用以下任一选项:

  • 使用
    CSS\u选择器

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul.autocomplete-content.dropdown-content li.active > span > span.highlight"))).click()
    
  • 使用
    XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='autocomplete-content dropdown-content']//li[@class='active']/span/span[@class='highlight' and text()='Hyderabad']"))).click()
    
  • 注意:您必须添加以下导入:

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

不要使用睡眠,使用等待。我从询问者发布的代码中复制了睡眠。如果愿意,他们可以将其更改为等待。raise exception_class(message,screen,stacktrace)selenium.common.exceptions.NoSuchElementException:message:没有这样的元素:找不到元素:{“method”:“xpath”,“selector”:“li[text()='hydrabad']”}(会话信息:chrome=76.0.3809.132)使用
span
而不是
li
更新了XPath,我最初误读了HTML,看起来城市文本在span元素中。仍然出现了相同的异常。找不到元素我尝试使用XPath编写代码,但出现了以下错误。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC