找不到div元素 - Selenium



我无法使用css_selector定位div元素。请在下面找到我的代码。

driver = wb.Firefox()
driver.get("https://www.jumia.com.ng/")
driver.maximize_window() #//For maximizing window
driver.implicitly_wait(20) #//gives an implicit wait for 20 seconds

#WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CLASS_NAME,'star _s')))
#driver.switch_to.frame(driver.find_element_by_class_name('star _s'))



#selecting phones and tablets
clickObj = driver.find_element_by_xpath("/html/body/div[1]/main/div[1]/div[1]/div[1]/div/a[4]/span").click()

#selecting mobile phones only
driver.find_element_by_xpath("/html/body/div[1]/main/div[2]/div[1]/div/article[1]/a[2]").click()

#selecting smartphones only
driver.find_element_by_xpath("/html/body/div[1]/main/div[2]/div[1]/div/article[1]/a[2]").click()

#selecting android phones only
driver.find_element_by_xpath("/html/body/div[1]/main/div[2]/div[1]/div/article[1]/a[1]").click()

product_info = driver.find_elements_by_css_selector("div.info")
product_name = list()
price = list()
rating = list()
for info in product_info:
#print(info.find_elements_by_class_name("rev")

product_name.append(info.find_element_by_css_selector("h3.name").text)
rating.append(info.find_element_by_css_selector("div.rev").text)
price.append(info.find_element_by_css_selector("div.prc").text)
#rating.append(info.find_element_by_class_name("rev").text)

data = {"product_name":product_name, "rating":rating, "price":price}
df_product = pd.DataFrame.from_dict(data)

它返回以下错误,尽管存在诸如"0"之类的元素;rev":

NoSuchElementException:消息:无法定位元素:div.rev

这是指向网站的链接https://www.jumia.com.ng/android-phones/

请协助。我做错了什么?

该问题是由于在find_elements_by_css_selector函数中没有考虑标记名和类名之间的空格。

product_info = driver.find_elements_by_css_selector("div .info")

此外,product_info变量的类型为<类"列表">。因此,不可能应用Selenium库的函数或方法。要填写其他列表,您必须使用product_info.text并为其申请循环。

参考以下代码作为示例:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.jumia.com.ng/android-phones/")
driver.maximize_window() #//For maximizing window
#selecting android phones only
product_info = driver.find_elements_by_css_selector("div .info")
for info in product_info:
try:
print(info.text)    
except:
break
driver.close()

最新更新