English 中文(简体)
• 如何确定使用硫酸ium的元素/清单作用?
原标题:How to fetch elements w/ listitem role using Selenium?

我试图通过一个网页中所载的几个清单项目进行周期。 我似乎无法成功地解脱这些因素。 I m试图打字的成分被界定为这样,网页上有4个:

<div data-test-component=“StencilReactView”=“listitem”类别=“cs-1uk1gs8”>

I m using the following in an attempt to fetch these elements (using Python): driver.find_elements(By.XPATH, "//div[@role= listitem ]")

一旦我能够成功掌握这一要素,我就能够按我的需要,通过这些要素进行循环,但我似乎无法成功掌握。 任何帮助都将受到高度赞赏。

我尝试通过XPATH和按班级挑选这个要素,但没有成功。

问题回答

鉴于超文本:

<div data-test-component="StencilReactView" role="listitem" class="css-1uk1gs8">

这些要素似乎是动态因素。


Solution

摘录:https://stackoverflow.com/a/64770041/7429447>>>>>>visibility_of_all_elements_ place_<><>>>>> • 定位器 战略:

  • 使用CSS_SlectTOR:

    elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "td.entity-name")))
    
  • 使用XPATH:

    elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@role= listitem ]")))  
    
  • http://www.un.org。 进口

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

或许,如果你要使用全部xpath来确定元素,就是指bsxpath

abs-xpath as this /html/one/div[3]/div/div/div/div/div/form/input, which from the 网站

您应制定这样的守则:

driver.find_element(By.XPATH, r"/html/body/div[3]/div/div/div/div/div[2]/form/input")
WebDriverWait(driver, timeout=10).until(EC.element_to_be_clickable((By.XPATH, r"/html/body/div[3]/div/div/div/div/div[2]/form/div[2]/div[2]/button")))
driver.find_element(By.XPATH, "/html/body/div[3]/div/div/div/div/div[2]/form/div[2]/div[2]/button").click()

<>strong>besides, 您可使用另一种方法查找元素,如:

driver.find_elements(By.CLASS_NAME, "username-input")[0]
driver.find_elements(By.TAG_NAME,  a )[0]
driver.find_elements(By.ID,  userSkin )[0]

the last, maybe you should use EC.* of selenium to wait for something happen you click and html reload, then some element can be located like login or logout some website, you should wait until page reload and some element can be located.

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, timeout=10).until(EC.element_to_be_clickable((By.XPATH, r"")))
WebDriverWait(driver, timeout=10).until(EC.visibility_of_all_elements_located((By.XPATH, r"")))
WebDriverWait(driver, timeout=10).until(EC.presence_of_element_located((By.XPATH, r"")))
WebDriverWait(driver, timeout=10).until(EC.title_contains((By.XPATH, r"")))
WebDriverWait(driver, timeout=10).until(EC.text_to_be_present_in_element((By.XPATH, r"")))




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签