It might not work finding the element because it's not visible in UI - it is loaded but not visible, the easiest way is to move to that element and click on it.
next_button = driver.find_element_by_css_selector('[aria-label=\'Next\']')
actions = ActionChains(driver)
actions.move_to_element(next_button).perform()
next_button.click()
Answer from Alin Stelian on Stack OverflowIt might not work finding the element because it's not visible in UI - it is loaded but not visible, the easiest way is to move to that element and click on it.
next_button = driver.find_element_by_css_selector('[aria-label=\'Next\']')
actions = ActionChains(driver)
actions.move_to_element(next_button).perform()
next_button.click()
next_button = driver.find_element_by_xpath('//button[@class="css-1lkjxdl eanm77i0"]').click()
You was using xpath variable and finding it by css. for css selector you have to use the class (.css-1lkjxdl) and use the above code it will work and accept the answer. Thanks!!
Hi all. I'm trying to use Selenium to find an element by CSS selector and although I've tried using the documentation, I think I'm misunderstanding something.
This is what I'm trying:
driver.find_element(By.CSS_SELECTOR,f"img [src='images/icons/new.gif'][alt='Add new item to list']").click()
This is the element I want to find and click:
<img src="images/icons/new.gif" alt="Add new item to list">
The error I get is as follows:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"css selector","selector":"img [src='images/icons/new.gif'][alt='Add new item to list']"}I can see it's on the page, however, so I'm not sure what I'm missing. I can find other items using XPATH and ID so selenium is up and running, just to rule that out. Can anybody suggest anything?
(I'm using selenium 4.8.2 and python 3.11.1, if that helps)
The beginning of the CSS Selector is wrong.
Searching for .freesim-text-last button span you will find two elements.

Pick the one which you're trying to reach using the follow:
.freesim-text-last button span:nth-of-type(1)
for the first element and
.freesim-text-last button span:nth-of-type(2)
for the second element.
In situations like this, you should try working up and seeing if the more easily available elements can be found instead. A fairly standard way check is to confirm the title element is found and correct.
Afterwards I would consider a timing issue. Has the DOM fully loaded before searching for this element? Selenium comes with different waits (implicit, explicit) which you can use to wait for certain elements to be loaded. A quick (and usually bad) way of testing this would just be to add:
time.sleep(5)
After you get open the browser, and before the element you're looking for, to give it time to load.
Try this
btn = driver.find_element_by_css_selector(".btn-standard.call-to-action")
You are missing a dot at the beginning of css_selector so it looks for an element btn-standard and not a class. And there is no such element as btn-standard
Also you can try element type with class like so:
btn = driver.find_element_by_css_selector("button.btn-standard")
Or any mix of class and element type
You can even use xpath:
btn = driver.findElement(By.xpath("//button[contains(text(),'Login')]"))
You should try the answer on this page How can I find an element by CSS class with XPath?.
You should be able to do something close to your first answer by searching only for "marker-cluster-small." Hope it helps in some way.
So it would be
("//div[contains(@class, 'marker-cluster-small')]")
You can also find it by css:
driver.find_element(:css, '.leaflet-marker-icon.marker-cluster.marker-cluster-small.leaflet-clickable.leaflet-zoom-animated')
But in this case, conor's answer is best since class value is too much lengthy.
Induce WebDriverWait and elementToBeClickable to click on the element.
Try the following options.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement selectGender = wait.until(ExpectedConditions.elementToBeClickable(By.id("id_gender1")));
selectGender.click()
OR
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement selectGender = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='radio-inline']/label[@for='id_gender1']")));
selectGender.click()
Here is your code
findElement(By.xpath("//input[@name='id_gender']")).click();
Im trying to challenge myself by making selenium redeem 1 gamepass code on microsoft issue is I Found the ID but it doesn't work as in Selenium can't find it, This is the website I need selenium to recognize and type in it
this is the error
Traceback (most recent call last):
File "c:\Users\jeans\Downloads\New folder\Microsoft\redeem.py", line 30, in <module>
gamepass = driver.find_element(By.ID, value="tokenString")
File "C:\Users\jeans\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 857, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\jeans\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 435, in execute
self.error_handler.check_response(response)
File "C:\Users\jeans\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="tokenString"]"}
(Session info: chrome=104.0.5112.81)
Stacktrace:
Backtrace:
Ordinal0 [0x00FA78B3+2193587]
Ordinal0 [0x00F40681+1771137]
Ordinal0 [0x00E541A8+803240]
Ordinal0 [0x00E824A0+992416]
Ordinal0 [0x00E8273B+993083]
Ordinal0 [0x00EAF7C2+1177538]
Ordinal0 [0x00E9D7F4+1103860]
Ordinal0 [0x00EADAE2+1170146]
Ordinal0 [0x00E9D5C6+1103302]
Ordinal0 [0x00E777E0+948192]
Ordinal0 [0x00E786E6+952038]
GetHandleVerifier [0x01250CB2+2738370]
GetHandleVerifier [0x012421B8+2678216]
GetHandleVerifier [0x010317AA+512954]
GetHandleVerifier [0x01030856+509030]
Ordinal0 [0x00F4743B+1799227]
Ordinal0 [0x00F4BB68+1817448]
Ordinal0 [0x00F4BC55+1817685]
Ordinal0 [0x00F55230+1856048]
BaseThreadInitThunk [0x76CEFA29+25]
RtlGetAppContainerNamedObjectPath [0x779B7A9E+286]
RtlGetAppContainerNamedObjectPath [0x779B7A6E+238]