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);
}
Related
I'm trying to open a website using Selenium, find a specific search box within it and fill it with a company name (MAGAZINE LUIZA S.A. in the example) and then click the "Search" button right next to the search box, since it will not work if I just hit "enter".
nav = webdriver.Edge()
nav.implicitly_wait(10)
nav.get('http://www.b3.com.br/pt_br/produtos-e-servicos/negociacao/renda-variavel/empresas-listadas.htm')
nav.find_element_by_xpath("/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[1]/label/span[1]/input[1]").send_keys('MAGAZINE LUIZA S.A.')
nav.find_elements_by_xpath('/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[2]/input').click()
And I get the Xpath of the search bar and search button by inspecting them and "copying full Xpath" in Microsoft Edge.
Problem is I get this error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[1]/label/span[1]/input[1]"}
(Session info: MicrosoftEdge=91.0.864.54)
But I have verified that I have copied and pasted the correct Xpaths for both the commands. Can anyone help?
You need to handle iframe, that XPath is relative to iframe.
This is your modified code
nav = webdriver.Edge()
nav.implicitly_wait(10)
nav.get('http://www.b3.com.br/pt_br/produtos-e-servicos/negociacao/renda-variavel/empresas-listadas.htm')
iframe = nav.find_element_by_xpath('/html/body/main/div[4]/div/div[1]/div[1]/div/form/section/div/div/div/iframe')
nav.switch_to.frame(iframe)
inp = nav.find_element_by_xpath("/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[1]/label/span[1]/input[1]")
inp.send_keys('MAGAZINE LUIZA S.A.')
nav.find_element_by_xpath('/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[2]/input').click()
I am trying to use Python to upload files to a website. But when I looked at the Developer Inspection Tool, there is no id, and no name. I am trying to use class name, but it does not work.
Inspect window from Selenium:
Inspect window from website:
I've tried methods like these:
upload = driver.find_element_by_class_name("icomoon icon-upload2 toolbar-button fileinput-button")
upload = driver.find_element_by_class_name("fileupload")
upload = driver.find_element_by_class_name("btnContainer").
All failed.
the error message:
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".btnContainer"}
(Session info: chrome=91.0.4472.77)
Then I use:
upload = driver.find_element_by_css_selector('body > div.fileupload')
upload.send_keys(r'c\user\xxx\upload\excel.xlsx')
#or
upload = driver.find_element_by_xpath("//div[#class='fileupload']")
upload.send_keys(r'c\user\xxx\upload\excel.xlsx')
The error message:
ElementNotInteractableException: Message: element not interactable
(Session info: chrome=91.0.4472.77)
Can anyone help me with this?
Thanks,
JH.
If you want a quick solution you can always try getting by ccs selctor. Right click on the element in the inspect widow. click on copy > selector. And then in python write driver.find_element_by_css_selector('paste the selector here')
This is not an elegant solution but it should work.
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"]
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()
I'm having a problem sending keys to a text field on an webpage written in angular JS.
Here is the WebElement in question:
<input name="CreateUserForename" id="Textc3829619-ad42-4df7-bbe3-5bdbe9fb9bce"
ng-class="{'ng-pristine': controller.$pristine, 'ng-invalid': controller.$invalid,
'ng-invalid-required': controller.$error.required, 'ng-valid': controller.$valid,
'ng-dirty': controller.$dirty}" class="form-input ng-scope ng-valid ng-dirty" type="text"
ng-if="!multiline" ng-hide="disabled" maxlength="">
Just using driver.find_element_by_xpath({xpath}).send_keys({keys}) gives this error:
Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="Text9c04f240-66a6-478b-92c2-13bb39379b8e"]"}
Same when using the css_selector.
One workaround I've found is using ActionChains and move_to_element_with_offset, but that is not ideal.
Any ideas?
(please don't suggest the Protractor)
OK, it seems that driver.find_element_by_name("CreateUserForename") does the trick.