I am trying to get "how to use" text value from this
I tried this css selector but I don't understand why I am not getting the text value
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
can anyone please help me?
CodePudding user response:
I hope you are scrolling the page before clicking the line
how_to_use = driver.find_element(By.LINK_TEXT, "How to use").click()
If so, to get the text you want to get you need to improve the locator and wait for that element to be visible.
I have no time to search for better locators there, but the following code is worked
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)
The output is:
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
CodePudding user response:
Not to intrude .. but why are you clicking stuffs, when it works without clicking? Here is a working example (setup on linux, just pay attention to imports and code after defining the browser):
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)
Result printed in terminal:
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.
CodePudding user response:
It may be possible that the text you are looking for has white spaced.
So, best practice will be:
how_to_use = driver.find_element(By.XPATH, "//*[contains(text(),'How To Use')]").click()
This will find the first element that has text the contains "How To Use". If you wish to find all elements use find_elements and then select the relevant index.