当我尝试在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>
2条答案
按热度按时间pnwntuvh1#
我认为你的错误在于你利用时间。睡觉。对于webscraper来说,使用sleep是非常糟糕的,因为不同的进程可能需要更长或更短的时间。这意味着该页尚未完全加载,程序已经开始计算数据。在Selenium中,有一个额外的函数允许您等待某些事情,在您的情况下,数据包含的主对象:
这个脚本正在等待,直到主元素可见,然后它将继续。这是执行程序时的输出:
fnvucqvd2#
你可以用这个
产出