我的元素生成一个空白输出(Selenium Python)

qacovj5a  于 2023-05-21  发布在  Python
关注(0)|答案(2)|浏览(142)

当我尝试在Selenium中打印元素时,它会输出一个空白。
我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from time import sleep

driver = webdriver.Chrome()
driver.get(input("Link Player: "))

sleep(0.5)
pace = driver.find_element(By.XPATH, "//div[@class='avg-stat']//span[contains(@class,'stat')]")
shooting = driver.find_element(By.XPATH, "//span[contains(text(),'Shooting')]/following-sibling::span")
passing = driver.find_element(By.XPATH, "//div[@class='avg-stat']//span[@class='name'][contains(text(),'Passing')]/following-sibling::span") # This element
agility = driver.find_element(By.XPATH, "//div[@class='avg-stat']//span[@class='name'][contains(text(),'Agility')]/following-sibling::span") # This element
defending = driver.find_element(By.XPATH, "//div[@class='avg-stat']//span[@class='name'][contains(text(),'Defending')]/following-sibling::span")# This element
physical = driver.find_element(By.XPATH, "//div[@class='avg-stat']//span[@class='name'][contains(text(),'Physical')]/following-sibling::span")# This element

print(shooting.text) # Only one that works is "pace"

和网站的一部分:(https://fifarenderz.com/22/player/30900061

<div data-v-b401ba9e="" class="avg-stat"><span data-v-b401ba9e="" class="name">Physical</span> <span data-v-b401ba9e="" class="stat stat-color-great">141</span> <div data-v-b401ba9e="" class="vs-progress--background vs-progress-#35a936" style="background: rgba(53, 169, 54, 0.1); height: 13px;"><div class="vs-progress--foreground" style="background: rgb(53, 169, 54); width: 100.714%;"></div><!----></div></div>
pnwntuvh

pnwntuvh1#

我认为你的错误在于你利用时间。睡觉。对于webscraper来说,使用sleep是非常糟糕的,因为不同的进程可能需要更长或更短的时间。这意味着该页尚未完全加载,程序已经开始计算数据。在Selenium中,有一个额外的函数允许您等待某些事情,在您的情况下,数据包含的主对象:

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

# maybe you dont need to add these options - in my case selenium wont open because of these errors.
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors-spki-list')
options.add_argument('--ignore-ssl-errors')
driver = webdriver.Chrome(chrome_options=options)

driver.get("https://fifarenderz.com/22/player/30900061")
timeout = 30

WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.XPATH, '//div[@class="w-1/2 md:w-1/3 xxl:w-1/6"]')))
pace = driver.find_element(By.XPATH, "//div[@class='avg-stat']//span[contains(@class,'stat')]").text
shooting = driver.find_element(By.XPATH, "//span[contains(text(),'Shooting')]/following-sibling::span").text
passing = driver.find_element(By.XPATH, "//div[@class='avg-stat']//span[@class='name'][contains(text(),'Passing')]/following-sibling::span").text
agility = driver.find_element(By.XPATH, "//div[@class='avg-stat']//span[@class='name'][contains(text(),'Agility')]/following-sibling::span").text
defending = driver.find_element(By.XPATH, "//div[@class='avg-stat']//span[@class='name'][contains(text(),'Defending')]/following-sibling::span").text
physical = driver.find_element(By.XPATH, "//div[@class='avg-stat']//span[@class='name'][contains(text(),'Physical')]/following-sibling::span").text

print("pace: ", pace)
print("shooting: ", shooting)
print("passing: ", passing)
print("agility: ", agility)
print("defending: ", defending)
print("physical: ", physical)

这个脚本正在等待,直到主元素可见,然后它将继续。这是执行程序时的输出:

pace:  115
shooting:  74
passing:  107
agility:  109
defending:  157
physical:  141
fnvucqvd

fnvucqvd2#

你可以用这个

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

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(chrome_options=options)

driver.get("https://fifarenderz.com/22/player/30900020")
timeout = 30

WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.XPATH, '//div[@class="w-full stat-row"]')))
pace = driver.find_element(By.XPATH, "//div[@class='avg-stat']//span[text()='Pace']//following-sibling::span").text
shooting = driver.find_element(By.XPATH,
                               "//div[@class='avg-stat']//span[text()='Shooting']//following-sibling::span").text
passing = driver.find_element(By.XPATH,
                              "//div[@class='avg-stat']//span[text()='Passing']//following-sibling::span").text
agility = driver.find_element(By.XPATH,
                              "//div[@class='avg-stat']//span[text()='Agility']//following-sibling::span").text
defending = driver.find_element(By.XPATH,
                                "//div[@class='avg-stat']//span[text()='Defending']//following-sibling::span").text
physical = driver.find_element(By.XPATH,
                               "//div[@class='avg-stat']//span[text()='Physical']//following-sibling::span").text

print("pace: ", pace)
print("shooting: ", shooting)
print("passing: ", passing)
print("agility: ", agility)
print("defending: ", defending)
print("physical: ", physical)

产出

pace:  138
shooting:  89
passing:  120
agility:  115
defending:  159
physical:  137

相关问题