简体   繁体   中英

Python Selenium select element a few questions

I have two questions.

  1. I'm trying to get the text value (Medium Coverage, Liquid Formula) But there are no locators. These text values are different from pages but the locations are the same. Is there any way to get this text like
find_element_by_class('css_s6sd4y.eanm7710') -> go down to one more row then select the element

在此处输入图片说明

  1. There is a class name. But I wonder if there is another way to find the element using data-at = 'sku_size_label'. And what is 'data-at' locator called?

在此处输入图片说明

for first question, you can use the below code :

to get the Medium Coverage :

wait = WebDriverWait(driver, 10)
ele = wait.until(EC.visibility_of_element_located((By.XPATH, "//img[@alt='Medium Coverage']/.."))).text
print(ele)

or

wait = WebDriverWait(driver, 10)
ele = wait.until(EC.visibility_of_element_located((By.XPATH, "//img[@alt='Medium Coverage']/.."))).get_attribute('innerHTML')
print(ele)

to get the Liquid Formula :

wait = WebDriverWait(driver, 10)
ele = wait.until(EC.visibility_of_element_located((By.XPATH, "//img[@alt='Liquid Formula']/.."))).text
print(ele)

or

 wait = WebDriverWait(driver, 10)
 ele = wait.until(EC.visibility_of_element_located((By.XPATH, "//img[@alt='Liquid Formula']/.."))).get_attribute('innerHTML')
 print(ele)

for your second question :-

Yes you can use data-at = 'sku_size_label' in xpath or css :

Xpath below :

//span[contains(@data-at, 'sku_size_label')]

CSS below :

span[data-at = 'sku_size_label']

and for this question :

what is 'data-at' locator called? - They are not called locators, they are basically attributes of the respective tag.

Imports :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM