简体   繁体   English

Python Selenium 选择元素的几个问题

[英]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'.但我想知道是否有另一种方法可以使用 data-at = 'sku_size_label' 查找元素。 And what is 'data-at' locator called?什么是“data-at”定位器?

在此处输入图片说明

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 或 css 中使用data-at = 'sku_size_label'

Xpath below :下面的Xpath:

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

CSS below : CSS如下:

span[data-at = 'sku_size_label']

and for this question :对于这个问题:

what is 'data-at' locator called?什么是'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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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