Locate an fill input element with selenium python - python

I'm Trying to fill the next txtBox but I'have tried with the xpath, name, class.
<input name="txtNumDoc" type="text" id="txtNumDoc" class="txtBox">
But always get me the same error.
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[contains(.,'txtNumDoc')]"}

First, the element you are trying to access is inside an iframe so you need to switch to that first:
iframe = driver.find_element_by_id("iframeBDUA")
driver.switch_to.frame(iframe)
and then you can find your element:
element = driver.find_element_by_id("txtNumDoc")

//*[contains(.,'txtNumDoc')] is not a valid xpath for this element as we don't have text txtNumDoc for this element. txtNumDoc is attribute value for both name and id in this case.
use the below.
//input[#id="txtNumDoc"]

Related

HTML Selenium Python click on href link

I would like to write a python code where he clicks on the href link.
Screenshot of HTML:
This is what I am currently having in Python, but it does not work.
tables = browser.find_elements_by_xpath('//*[#id="notice"]')
for table in tables:
row = table.find_element_by_xpath('//tr[#class="Zebra"]//td//a[#href="https://enot.publicprocurement.be/enot-war/preViewNotice.do?noticeId=438868&saveSearchParams=true&pageSize=%31%32%35&d-446978-p=%31&"]').click()
This is the error message for row:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//tr[#class="Zebra"]//td//a[#href="https://enot.publicprocurement.be/enot-war/preViewNotice.do?noticeId=438868&saveSearchParams=true&pageSize=%31%32%35&d-446978-p=%31&"]"}
Could you please tell me what I am doing wrong?
The desired element is a <a> element with the innerText as Universiteit Antwerpen-22004
To click on the desired element you can use either of the following locator strategies:
Using the href attribute:
table.find_element_by_xpath('.//tr[#class="Zebra"]//td//a[starts-with(#href, "https://enot.publicprocurement.be/enot-war/preViewNotice.do?noticeId")]').click()
Using the innerText:
table.find_element_by_xpath('.//tr[#class="Zebra"]//td//a[contains(., "Universiteit Antwerpen-22004")]').click()

Unable to locate an element in a span class using Python Selenium

I have been struggling to locate an element in a span class. The element is a radio-checkmark button. Here is the html:
<span class="radio-container" for="searchType_2">
<input class="form-check-input" type="radio" name="searchType" id="searchType_2" value="cidade">
<span class="radio-checkmark">
::after
As the classes above are not unique, I tried the following:
dropdown_menu = self.driver.find_element_by_css_selector('[for="searchType_2"] .radio-checkmark')
When I do the inspection and search using the CSS selector above it works. It shows me as 1 of 1. But when I run the code, I get the following exception:
no such element: Unable to locate element: {"method":"css selector","selector":"[for="searchType_2"] .radio-checkmark"}
(Session info: chrome=92.0.4515.107
Thanks
The element you are trying to access is inside an iframe. You need to switch to the iframe before accessing any element inside it
driver.switch_to_frame(driver.find_element_by_xpath("//iframe[#class='cz-map-frame']"))
driver.find_element_by_xpath("//input[#id='searchType_2']//following::span[#class='radio-checkmark'][1]").click();

Why can't I find element using class name?

I am trying to find this button and click on it.
But I get this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".sqdOP L3NKy y3zKF "}
(Session info: chrome=87.0.4280.88)
I have noticed that it is looking for a different string '.sqdOP L3NKy y3zKF' bc it adds this point in front of the class name. Is this the problem?
<button class="sqdOP L3NKy y3zKF " type="button">Follow</button>
Thank you!
You can also locate it with this if there isn't a button with the same class name:
driver.find_element_by_class_name("sqdOP")
Selenium doesn't recognize spaces in class names (unless you do something like what Villa_7 said) because those are actually "compound classes" (see this post and this one). It has something to do with CSS, I believe.
Fyi, it'd be helpful to see code for how you're currently trying to select the button.
If classname value contains spaces, Selenium cannot locate it via dot "."
You have to use this construction:
"[class='sqdOP L3NKy y3zKF ']"
Or just try to locate by visible text using XPath:
"//button[text()='Follow']"
If you're getting Element click intercepted exception, just try to click via JS, like this:
public void executeClickJS(WebDriver driver, WebElement webElement) {
((JavascriptExecutor) driver).executeScript("arguments[0].click();", webElement);
}

How to get element by attribute with Selenium, Xpath and Expected Condition

This is what I'm using:
getByAttribute = WebDriverWait(amazonDriver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[#an-attribute='data-category']")))
The element looks as follows:
<div class='nav-subnav' data-category='drugstore'>
This is present on every Amazon products page.
It times out and does not find the element.
Use #data-category to get element by attribute.
getByAttribute = WebDriverWait(amazonDriver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[#data-category]")))
CSS Selector:
getByAttribute = WebDriverWait(amazonDriver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data-category]")))

How can I click on span button which has no lisk with selenium webdriver Python?

This is my HTML code:
<button class="_qv64e _gexxb _r9b8f _njrw0">Follow</button>
But, when I try to click on this this error is occurred:
selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: span._qv64e _gexxb _r9b8f _njrw0
It is happened in any way that I used, for example in xpath, CSS selector, tag name and ... . This error has been occurred through this code:
driver.find_element_by_css_selector("span._qv64e _gexxb _r9b8f _njrw0").click()
When using css_selector spaces has meaning. The selector you are using tell the driver to look for an element with <_njrw0> tag which has an ancestor with <_r9b8f> tag as so on. The button also has <button> tag, not <span> tag.
You need to use . in front of any class name and without spaces
driver.find_element_by_css_selector("button._qv64e._gexxb._r9b8f._njrw0").click()

Categories

Resources