简体   繁体   English

有人可以帮我弄清楚如何打印此下拉列表中的选项吗?

[英]Can someone help me figure out how to print the options in this dropdown?

I've been trying to use selenium so I can list the available size options in this webpage but I am having difficulty identifying the class element.我一直在尝试使用 selenium 以便我可以在此网页中列出可用的尺寸选项,但我很难识别 class 元素。 Every time I use main-size-select-0, I get an error saying unable to locate element.每次我使用 main-size-select-0 时,都会收到一条错误消息,提示无法定位元素。 I have even tried using xpath but it didn't work either.我什至尝试过使用 xpath 但它也不起作用。

Here is my code below:下面是我的代码:

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup

driver = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')
driver.get("https://www.asos.com/nike/nike-air-max-95-logo-leather-trainers-in-dark-navy-orange/prd/20750072?colourwayid=60085113")
select = Select(driver.find_element_by_id("main-size-select-0"));

One of the main problems may be that your code is looking for the menu before it's fully loaded.主要问题之一可能是您的代码在完全加载之前正在寻找菜单。 You can use WebDriverWait and EC to hold the code until the element is loaded, then proceed.您可以使用WebDriverWaitEC来保存代码,直到加载元素,然后继续。 It doesn't matter too much how you find the element, I used xpath, but you can do whatever you want.如何找到元素并不重要,我使用的是 xpath,但你可以为所欲为。 Once you get it the .text method returns everything you need.一旦你得到它, .text方法就会返回你需要的一切。

I split the text on newline and eliminated anything out of stock as well as the first item in the list which is 'Please select'.我在换行符上拆分文本并消除任何缺货以及列表中的第一项“请选择”。

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


driver = webdriver.Chrome()
driver.get("https://www.asos.com/nike/nike-air-max-95-logo-leather-trainers-in-dark-navy-orange/prd/20750072?colourwayid=60085113")
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="main-size-select-0"]')))
dd = driver.find_element_by_xpath( '//*[@id="main-size-select-0"]')

[x for x in dd.text.split('\n') if not any(w in x for w in ['Please','Not'])]

Output Output

['UK 6',
 'UK 7',
 'UK 7.5',
 'UK 8',
 'UK 8.5',
 'UK 9',
 'UK 9.5',
 'UK 10',
 'UK 10.5',
 'UK 11',
 'UK 12',
 'UK 13']

A few things...一些东西...

  1. The items in the list are loaded a bit later than when the page loads.列表中的项目加载时间稍晚于页面加载时间。 I'd place a time.sleep() or WebDriverWait after your driver.get([URL]).我会在你的 driver.get([URL]) 之后放置一个 time.sleep() 或 WebDriverWait。

  2. To get a list of elements in the drop down, you'll want to set your xPath to the "option" tag under "main-size-select-0".要获取下拉列表中的元素列表,您需要将 xPath 设置为“main-size-select-0”下的“option”标签。 For Chrome, that would be: //*[@id="main-size-select-0"]/option Access these using driver.find_element s _by_xpath which would return a list of all the items in the drop down list对于 Chrome,这将是: //*[@id="main-size-select-0"]/option 使用driver.find_element s _by_xpath访问这些将返回下拉列表中所有项目的列表

To summarize...总结...

...
import time

driver = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')
driver.get("https://www.asos.com/nike/nike-air-max-95-logo-leather-trainers-in-dark-navy-orange/prd/20750072?colourwayid=60085113")
time.sleep(5)
select_list = Select(driver.find_elements_by_xpath("//*[@id="main-size-select-0"]/option"));

Note that the xPath generated there was done via Chrome.请注意,在那里生成的 xPath 是通过 Chrome 完成的。 It might differ slightly for Firefox. Firefox 可能略有不同。 Just ensure you don't access a particular item in the list ie: //*[@id="main-size-select-0"]/option[0]只要确保您没有访问列表中的特定项目,即: //*[@id="main-size-select-0"]/option[0]

暂无
暂无

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

相关问题 有人能帮我算出算法的时间复杂度吗? - Can someone help me figure out the time complexity of my Algorithm? 我不知道如何解决这个问题,有人可以帮助我吗? Pirple作业python - I cannot figure out how to fix this problem, can someone help me ? Pirple homework python 有人可以帮我将 **for 循环** 替换为 **while 循环** 我正在努力弄清楚吗? - Can someone help me replace the **for loop** to **while loop** I'm struggling to figure it out? 有人可以帮我弄清楚为什么它一直说 largest 是未定义的吗? 最小的运行良好 - Can someone help me figure out why it keeps saying that largest is undefined? Smallest runs fine 我对这个刽子手程序有点麻烦。 有人可以帮我弄清楚吗? - I am having a bit of trouble with this hangman program. Can someone help me figure it out? 有人可以帮我弄清楚如何在我的 python 刽子手代码上更新我的破折号 - May someone help me figure out how to update my dashes on my python hangman code 我正在通过字典建立关系,但是有人可以帮我解决一个我不知道的输出吗? - I am making relationships from a dictionary, but can someone help me fix one output I can't figure out? 当我有两个具有不同 url 的不同嵌入命令时,只有其中一个可以工作 - 有人可以帮我解决这个问题吗? - When I have two different embed commands with different urls only one of them will work- Can someone help figure this out for me? 有人可以向我解释此Python代码中我在做什么错,并帮我弄清楚吗? - Could someone explain to me what I'm doing wrong in this Python Code and help me figure it out? 如何验证护照。 有人能帮我吗? - How to verify a passport . Can someone help me?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM