I want to get elements from selenium, but I can't choose the latter because there are two of them with the same name.

Asked 2 years ago, Updated 2 years ago, 336 views

<a href="URL" target="_blank" class="button button-color-2">New membership registration</a>
<a class="button">Login </a>

Here,

driver.find_element_by_css_selector('.button').click()

selenium

2022-09-30 21:50

1 Answers

Specify multiple elements mentioned in the comment, or use of scraping (2) selenium in Python to explain the following:

Specify multiple elements

Specify multiple elements
Command to retrieve multiple specified elements.
The retrieved value is returned in list format.
Basically, all you have to do is change the above command to element → elements.

Specify in the css selector

driver.find_elements_by_css_selector("CSS selector")

How to use selenium in Python (2)

Getting Multiple Elements
If you specify an element by tag name or class name, you can take multiple elements.
In that case, specify the index of the elements you want to retrieve in the array with the "find_element" part as "find_element."

elem2=browser.find_elements_by_class_name('post-content')
elem2[1].text

If you apply the above, for example, wouldn't it be possible to do the following?

driver.find_elements_by_css_selector('.button')[1].click()

Other than that, this article has not been resolved, but it has a +1 response.

How to specify python selenium class

If you use python selenium to specify a class, assume that there are more than one class:

<div class="hoge"></div>
<div class="hoge"></div>
<div class="hoge"></div>

For example, if you want to click on the second class from this class, could you tell me how to specify it?

Is it like this?

driver.find_elements_by_css_selector(".hoge:nth-of-type(2)").click()

Try it.


2022-09-30 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.