Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
73 views
in Technique[技术] by (71.8m points)

Selenium(Python) alternative to for loop with index

Is there a more efficient way of reading classes with the same name, rather than doing what im doing, i basically go through a code with index, because they all have the same name, is there a better way if selecting elements with the same class name, rather than the way i am doing it

Z = len(driver.find_elements_by_class_name('Class_With_Names'))

for x in range(Z):

x += 1

thisClass = driver.find_element_by_xpath(f"//ul[@class='same_name']/div[{str(x)}]")
classText = thisClass.find_element_by_class_name("class_with_text")

print(classText.text)
question from:https://stackoverflow.com/questions/65838618/seleniumpython-alternative-to-for-loop-with-index

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have you tried using the find_elements_by_xpath? This will return all of the elements with that class name.

Your code looks like it is incrementing after the div though? Are you just looking for information on all of the classes or to loop through the divs?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...