Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

python - Whats is the correct element type for this selenium project?

I'm trying to find something out but nothing works. I'm trying to use a web scraper, to print all of the hot deal percentages on this website: ' https://shadowpay.com/en?price_from=0.00&price_to=34.00&game=csgo&hot_deal=true ' But I ran into an error (I have tried many ways to go about this, but I think that it's my lack of HTML) The error is that the element that I want to print ('percent-hot-deal__block'), isn't a class name, but I've tried a lot of find_element_by options, nothing worked, so I'm coming here. Code:

import pandas as pd
from bs4 import BeautifulSoup as bs
from selenium import webdriver
import requests
import time
#
perc = []
#
PATH = 'C:/Users/Matiss/Documents/chromedriver_win32/chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get("https://shadowpay.com/en?price_from=0.00&price_to=34.00&game=csgo&hot_deal=true")
#
dealblock = driver.find_elements_by_tag_name("span")
for deal in dealblock:
    header = deal.find_element_by_class_name("percent-hot-deal__block")
    print(header.text)
#
time.sleep(15)

driver.quit()

Please help, if I need to edit anything, be sure to comment. Also, I know that importin so many things is useless, I was following other tutorials on the same file.

question from:https://stackoverflow.com/questions/65868329/whats-is-the-correct-element-type-for-this-selenium-project

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Basically you want to wait for the page to load and grab the element.

You can do something like this.

driver.get("https://shadowpay.com/en?price_from=0.00&price_to=34.00&game=csgo&hot_deal=true")
driver.implicitly_wait(5)
dealblock = driver.find_elements_by_css_selector("span.percent-hot-deal__block")
#print(len(dealblock))
for deal in dealblock:
    print(deal.text)

The more ideal way is:

wait = WebDriverWait(driver, 10)
driver.get("https://shadowpay.com/en?price_from=0.00&price_to=34.00&game=csgo&hot_deal=true")
dealblock = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "span.percent-hot-deal__block")))
#print(len(dealblock))
for deal in dealblock:
    print(deal.text)

Imports

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...