我正在尝试从该页面获取附加组件。如果项目可定制
在收集完所有的divs
之后,我尝试转到页面顶部(不起作用)。我检查可定制的div
是否存在。如果是这样,请滚动到视图中,单击Add按钮并获取Add-on详细信息。我试过了,actionChains
,没用execute_script
也不做任何事情。以前的点击是有效的,但是页面上下滚动有点搞砸了。几个小时来,我一直在努力把这件事做好。非常感谢您的帮助
for i in range(6):
item_dvs = driver.find_elements_by_class_name('_2wg_t')
driver.execute_script("window.scrollBy(0, 3100)")
driver.execute_script("window.scrollBy(0,-18500);")
for div in item_dvs:
try:
name = div.find_element_by_class_name('styles_itemNameText__3bcKX')
price = div.find_element_by_class_name('rupee')
if div.find_elements_by_class_name('styles_itemDesc__MTsVd'):
desc = div.find_element_by_class_name('styles_itemDesc__MTsVd').text
else:
desc = None
is_cust = div.find_element_by_class_name('_1C1Fl _23qjy')
if is_cust:
driver.execute_script("arguments[0].scrollIntoView();", is_cust)
首先,我建议您使用
WebDriverWait
,因为当元素不存在时,行if div.find_elements_by_class_name('styles_itemDesc__MTsVd')
和is_cust = div.find_element_by_class_name('_1C1Fl _23qjy')
将引发异常。使用WebDriverWait
的另一个原因是等待Add按钮可单击。这里是两个问题的解决方案,您可以使用try/catch来处理描述和“customizable”元素的存在,并且可以等待Add按钮可单击:相关问题 更多 >
编程相关推荐