我正在嘗試從此
我嘗試了這個 css 選擇器,但我不明白為什么我沒有得到文本值
how_to_use = driver.find_element(By.LINK_TEXT, "How to use").click() #clicking on How to use section
how_to_use = driver.find_element(By.CSS_SELECTOR,"#qqrco4-accordion div").text #trying to get how to use section text
誰能幫幫我嗎?
uj5u.com熱心網友回復:
我希望您在單擊該行之前滾動頁面
how_to_use = driver.find_element(By.LINK_TEXT, "How to use").click()
如果是這樣,要獲得您想要獲得的文本,您需要改進定位器并等待該元素可見。
我沒有時間在那里搜索更好的定位器,但是下面的代碼是有效的
import time
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.sephora.ae/en/p/nude-obsession-lip-kit-P10043065.html"
driver.get(url)
wait = WebDriverWait(driver, 10)
driver.execute_script("window.scrollBy(0, arguments[0]);", 600)
time.sleep(1)
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "How to use"))).click()
time.sleep(1)
content = driver.find_element(By.CSS_SELECTOR, '.tabs-panel.is-active div').text
print(content)
輸出是:
C:\Users\*****\PycharmProjects\test\venv\Scripts\python.exe C:/Users/*****/PycharmProjects/test/so.py
Step 1: Define
Artist Color Pencil:
Matte, highly pigmented, and easy glide pencil to define & correct the lip shape.
Step 2: Color
Rouge Artist:
Exquisite gliding & comfortable texture with moisturizing effect for 24 hours.
Process finished with exit code 0
uj5u.com熱心網友回復:
不要打擾..但是當它不點擊就可以作業時,您為什么要點擊東西?這是一個作業示例(在 linux 上設定,定義瀏覽器后只需注意匯入和代碼):
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
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,720")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(browser, 20)
url = 'https://www.sephora.ae/en/p/nude-obsession-lip-kit-P10043065.html'
browser.get(url)
how_to_use_text = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text() = 'How to use']/ancestor::a/following-sibling::div")))
print(how_to_use_text.text)
終端列印的結果:
Step 1: Define
Artist Color Pencil:
Matte, highly pigmented, and easy glide pencil to define & correct the lip shape.
Step 2: Color
Rouge Artist:
Exquisite gliding & comfortable texture with moisturizing effect for 24 hours.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/506703.html
標籤:Python python-3.x 硒 硒网络驱动程序
上一篇:找不到正確的XPATH
