I am using selenium and python to learn about automation web testing.
I want to click on "Continue" button, while there is only span in it (I had learned that using id instead of span is so much easier)
but in this case, I want to click the span.
I am using below code:
driver.find_element_by_xpath("//span[contains(#class,'mdBtn01Txt')][contains(text(),'Continue')]").click()
here is the element :
<div class="mdLYR12Inner01">
<a class="MdBtn01 mdBtn01 ExDisabled FnSubmitBtn" style="display:none" href="#" onclick="charge(this); return false;">
<span class="mdBtn01Inner">
<span class="mdBtn01Txt">
Continue <<<(I want to click this button)
</span>
</span>
</a>
</div>
</footer>
But, I got this message below:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(#class,'mdBtn01Txt')][contains(text(),'Continue')]"}
Does not looks like a valid xpath, correct one should be
//span[#class='mdBtn01Txt']
Here is the rule to create xpath
Syntax for XPath:
XPath contains the path of the element situated at the web page. Standard syntax for creating XPath is:
Xpath=//tagname[#attribute='value']
Tagname: Tagname of the particular node.
#: Select attribute.
Attribute: Attribute name of the node.
Value: Value of the attribute.
Related
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();
I'm a newbie in coding with python and selenium. I try to find an element in a web-page by its class name.
<input type="email" class="whsOnd zHQkBf" jsname="YPqjbf"
autocomplete="username" spellcheck="false" tabindex="0"
aria-label="Adresse e-mail ou numéro de téléphone" name="identifier"
value="" autocapitalize="none" id="identifierId" dir="ltr"
data-initial-dir="ltr" data-initial-value="">
But each time I've tried I have this error.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".whsOnd zHQkBf"}
I don't understand why because I verified and I think that it's not in an iframe so I don't understand why do I have this message and I don't know how to manage the error.
Here are the pictures of my code and the html :
My code
The HTML of the page
Hard to tell without seeing your code and the html. Are you sure it is not in a iframe?
If not you can perhaps try to use xpath to find the element.
When inspecting the webpage in chrome you can right click the element and say copy -> copy xpath. I do recommend looking into xpath, so you know what you are searching for.
Edit:
Try using only one class name, since find_element_by_class_name() only accepts one.
This:
driver.find_element_by_class_name("zHQkBf")
instead of this:
driver.find_element_by_class_name("whsOnd zHQkBf")
I've been trying to build a bot that scrapes the name of followers liking a picture in instagram. The website opens a popup box with the accounts in side, and the box seemingly refreshes the account links as you scroll it. I've written a code that will open the box and then scroll it, but i can't get selenium to scrape the account names. My code looks like this to scroll the pop up box:
realscroll_box = browser.find_element_by_xpath('/html/body/div[4]/div/div[3]/div')
while last_ht != ht:
last_ht = ht
time.sleep(2)
ht = browser.execute_script('''
arguments[0].scrollTo(0, arguments[0].scrollHeight);
return arguments[0].scrollHeight;
''', realscroll_box)
namelinkstemp1 = realscroll_box.find_elements_by_xpath('//*[contains(#href,"/")')
But it gives me this error:
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //[starrts-with(#href,"/") because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[starrts-with(#href,"/")' is not a valid XPath expression.
The text i'm trying to extract looks like this:
<a title="instagramusername" href="/instagramusername/">
<div class=" Igw0E IwRSH eGOV_ ybXk5 _4EzTm ">
<div class="_7UhW9 xLCgt qyrsm KV-D4 fDxYl rWtOq">
<div class=" Igw0E rBNOH eGOV_ ybXk5 _4EzTm ">instagramusername</div>
</div>
</div>
</a>
Any help is appreciated, xpath queries are very new to me.
Thanks :)
Starts-with function finds the element based on attribute value. In your code function is not correct . Also //*[contains(#href,"/") , here you missed ]
Correct one : //a[contains(#href, '/')]
//*[starts-with(#href,'/')]
or
//a[starts-with(#href,'/instagramuse')]
Using Python 3+ and Selenium > IE Driver
I am running a python script to automate filling out a form on a page. The reason for finding the parent element is because there are multiple elements with the same class and no other identifiers other then the text based title.
HTML:
<div class="pg_BoxContents"> /* Div I am trying to select */
<div class="Title">Replay Permissions:</div>
</div>
Python / Selenium:
replay_form = browser.find_element_by_xpath("//span[contains(text(),'Replay Permissions:')]/ancestor::div[contains(#class, 'pg_BoxContents')]")
Error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with xpath == //span[contains(text(),'Details:')]/ancestor::div[contains(#class, 'pg_BoxContents')]
Replace:
//span[contains(text(),'Replay Permissions:')]/ancestor::div[contains(#class, 'pg_BoxContents')]
with:
//div[contains(text(),'Replay Permissions:')]/ancestor::div[contains(#class, 'pg_BoxContents')]
You are using //span[... where there is no span in HTML you have provided. But there is a div.
HTML information is :
<a title="Create an Account" class="button" href="http://demo.magentocommerce.com/customer/account/create/">
<span>
<span>Create an Account
</span>
</span>
</a>
Create an Account
I am trying with :
create_account_button = driver.find_element_by_xpath("//button[#title='Create an Account']")
create_account_button.click()
but it is not working
Actually assign the WebElement variable, u use the following code
WebElement button = driver.findElement(By.xpath("//a[#title='Create an Account']");
button.click();
Steps
Creating a WebElement variable button and assigning the value to it.
Performing click() on that webelement
For Java follow above steps, for Python use below
driver.find_element_by_xpath('//a[#title='Create an Account']').click()
Xpath you need To use be in <a> not in <button>
So try this Xpath //a[#title='Create an Account']