selenium Selify Python中一组元素中的For循环

k0pti3hp  于 2022-11-10  发布在  Python
关注(0)|答案(1)|浏览(171)

我正试着让我的一项手工任务自动化。我已经写好了完整的脚本,但我陷入了for Loop无法工作的状态。
以下是我想做的事情:
这是我想要循环并执行一些操作的一组卡片。出于测试目的,我只是尝试打印所有产品的标题。

这是之前附上的图片的主要代码。Class=‘m--t--1’是包含所有这些产品的主要元素。

下面是单个产品的代码。

现在,这是我写的代码。注意:这不是完整的脚本。我刚写完剧本中我遇到问题的那部分。


# Logging into the website with credentials and saved cookies!

driver.get("https://poshmark.com/login")
time.sleep(5)
driver.maximize_window()
time.sleep(5)
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)

time.sleep(5)
driver.find_element_by_xpath('//*[@id="login_form_username_email"]').send_keys('email hidden')
time.sleep(5)
driver.find_element_by_xpath('//*[@id="login_form_password"]').send_keys('password hidden')
time.sleep(5)
driver.find_element_by_xpath('//*[@id="email-login-form"]/div[4]/button').click()

# Navigating to the closet where these products can be found.

driver.find_element_by_xpath('//*[@id="app"]/header/nav[1]/div/ul/li[5]/div/div[1]').click()
driver.find_element_by_xpath('//*[@id="app"]/header/nav[1]/div/ul/li[5]/div/div[2]/ul/li[1]/a').click()

container_of_products = driver.find_elements_by_class_name('tile col-x12 col-l6 col-s8 p--2')

for item in container_of_products:
    listing = item.find_element_by_tag_name('a')
    print(listing.text)
    driver.execute_script("window.history.go(-1)")
dfddblmv

dfddblmv1#

Tile ol-x12 ol-16 ol-s8 p--2是一个复合类名,它不会以这种方式工作。使用一个css选择器和.tile.ol-x12.ol-l6.ol-s8.p--2

container = driver.find_elements_by_css_selector('.tile.col-x12.col-l6.col-s8.p--2')

相关问题