selenium XPath使用Selify提供空输出

flvlnr44  于 2022-11-10  发布在  其他
关注(0)|答案(3)|浏览(127)

我没有得到他们给我的空输出这是页面链接https://www.amazon.com/dp/B00M0DWQYI?th=1

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd

url='https://www.amazon.com/dp/B00M0DWQYI?th=1'
PATH="C:\Program Files (x86)\chromedriver.exe"
driver =webdriver.Chrome(PATH)
driver.get(url)
item=dict()
try:
    item['price'] = driver.find_element(By.XPATH, "//div[@id='corePrice_feature_div'] //span[@class='a-offscreen']").text
except:
    item['price']=''

print(item)
7xllpg7q

7xllpg7q1#

在定位该元素之前,您可能需要等待该元素正确加载:

[...]
wait = WebDriverWait(driver, 10)

item['price'] = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@id='corePrice_feature_div']//span[@class='a-offscreen']"))).text

selenium 文档可在https://www.selenium.dev/documentation/上找到
编辑:下面是一个完整的例子,告诉你如何获取这些信息:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time as t 

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1920,1080")

webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
driver = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(driver, 5)

items = dict()

driver.get('https://www.amazon.com/dp/B00M0DWQYI?th=1')
t.sleep(1)
driver.refresh()
items['price'] = wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@id="corePrice_feature_div"]//span[@class="a-price aok-align-center"]'))).text.replace('\n', '.')
print(items)

结果为终端:

{'price': '$32.98'}
cngwdvgl

cngwdvgl2#

您需要等待元素可见,然后提取其文本。
以下Selify代码可以正常工作:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = 'https://www.amazon.com/dp/B00M0DWQYI'
driver.get(url)
wait = WebDriverWait(driver, 10)

print(wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@id='corePrice_feature_div']"))).text)

输出为

$32
98
vwkv1x7d

vwkv1x7d3#

你可以使用BS4,它会工作得很好

from bs4 import BeautifulSoup
soup = BeautifulSoup(driver.page_source, 'lxml')

try:
    item['price'] = soup.find('input', id="attach-base-product-price").get('value')
except:
   item['price'] = ''
   finally:
           driver.close()
           driver.quit()

打印(项目)

相关问题