got a problem with my code and I need your help. What I'm trying to do is the following:
1- access a website;
2- fill the registration form: name, email, password, etc.
Step 1 works; after clicking the sign up button, the form will pop up in a new tab.
Step 2; when trying to find the elements, by, id or name, I get the error "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element"
My code is the following:
driver.find_element_by_link_text('Sign Up').click()
time.sleep(3)
driver.find_element_by_id("signup_title").send_keys(signup_tile)
driver.find_element_by_id("signup_name").send_keys(signup_name)
Attached you can find the html. Thank you in advance, much appreciated your help.
Note
In console I tried to find the element searching the id using
$x("//*[#id='signup_title']") and it was found: [input#signup_title.sc-AxirZ.kzqQJb.invalid]. Also the element is loaded.
HTML
Try using:
driver.find_element_by_xpath("<XPath>")
It seems to work better. To get the XPath of an element, find the element in the inspector, right-click it then Copy -> Copy XPath. If that doesn't work, select Copy Full XPath instead
The problem was related to the second tab; because the form opens in another tab, the window must be switched in the code.
I used the following:
driver.switch_to.window(driver.window_handles['Nr']), Nr being the index for the tab- if there are 2, the main one, and the second one-in my case with the form, the index will pe 1-counting starts from zero.
Related
I am trying to click on "OK" button on a pop up using selenium and Python but i face an error "no such element: Unable to locate element" although being sure that my id is 100% correct.
> <a class="dxm-content dxm-hasText dx dxalink" href="javascript:;" role="menuitem" id="Dialog_PAC_Menu_DXI0_T"><span class="dx-vam dxm-contentText">OK</span></a>
My python selenium code:
Export2 = driver.find_element(By.XPATH,'//a[#id="Dialog_PAC_Menu_DXI0_T"]')
Export2.click()
Where exactly did I go wrong, i also tried full Xpath, wait till clickable, time sleep. everything!
i would appreciate if someone can help me with it.
Well, it looks like you found the answer and that was because your element was inside an iFrame so I will just post an answer here so others can find an easy answer if they view your question.
tmpheader = driver.find_element(By.CSS_SELECTOR, "#modal > iframe")
driver.switch_to.frame(tmpheader)
Export2 = driver.find_element(By.XPATH,'//a[#id="Dialog_PAC_Menu_DXI0_T"]')
Export2.click()
This switches our current driver that we are looking for into the first iFrame element on the page. If there is more than one iFrame you would need to check for more attributes in the first line to make sure you are looking at the correct one
Actually that's how I solved my issue as my element was inside an iFrame as John Gordon suggested.
driver.switch_to.frame(driver.find_element(By.TAG_NAME,'iframe'))
slider = driver.find_element(By.XPATH,"//*[contains(#id,'_xaf_dviImportExportFormat_Edit_dropdown_DD_B-1Img')]").click()
Today I'm having troubles due to a "a href" button that does not have any ID to be identified, then let's explain a little bit more about the problem... I have an structure like this one(let's assume XXX is an anonymous path):
wait = WebDriverWait(driver, 5)
el=wait.until(EC.presence_of_element_located((By.ID, 'XXX1')))
entries = el.find_elements_by_tag_name('tr')
for i in range(len(entries)):
if(entries[i].find_element_by_xpath(XXX2).text==compare):
el = wait.until(EC.element_to_be_clickable((By.ID,XXX3)))
el.click()
el=wait.until(EC.presence_of_element_located((By.ID, XXX4)))
entries2 = el.find_elements_by_tag_name('tr')
for j in range(len(entries2)):
#Some statements...
xpath = ".../a"
your_element=WebDriverWait(driver,10)\
.until(EC.element_to_be_clickable((By.XPATH,xpath)))##Here the problem
your_element.click()
Then, I'm getting information from an hibrid page (dynamic and static) using as driver a ChromeDriver one, once I get a big table, inside every row there is a button that shows more info, then I need to click it for open it too, the main problem is when this operation iterates, that error is shown by the output. This driver is a ChromeDriver one. In summary, first I search something and click on search button, then I get a table where every row(at the end in the last column) has a button that once is open, shows more information, consequently I need to open it and close it due to the next row, it works with the first row, but with the second one, it crashes. I would really appreciate any advice of how to handle this problem.
Thanks in advance!
The problem is that you change with the click the dom within your loop. For mr this never worked.
One solution is, to try to re-query within the loop to make sure your at the correct position. Your third line:
entries = el.find_elements_by_tag_name('tr')
should be executed every time and with a counter make sure you are at the correct position of your <tr> entries.
After searching for a while an answer to my question, I couldn't get an answer that helped me, so here I am asking for your help ! :)
Right now, I am trying to select a plan on a website page which, after it has been selected (Read : a certain button clicked) displays the rest of the page where I can send the keys / values that I want to send.
Here is the code I am using
select_plan = browser.find_elements_by_xpath(".//*[#id='PostAdMainForm']/div[1]/div/div/div/div/div[1]/div[3]/button")
select_plan.click()
I found the xpath with Firepath, but when I run my code it gives me a AttributeError: 'list' object has no attribute 'click'
Here is the page I am trying to click from
https://www.kijiji.ca/p-post-ad.html?categoryId=214&hpGalleryAddOn=false&postAs=ownr
(I am looking to click on the left button, the one in blue)
Thank you very much for you help :)
The method find_elements returns a list, not a single element. You are taking the result and trying to click on it. Like the error says, you can't click on a list.
Either use find_element (singular) or use find_elements (plural) and then click on one of the elements that was returned.
# using find_elements
select_plans = browser.find_elements_by_xpath(".//*[#id='PostAdMainForm']/div[1]/div/div/div/div/div[1]/div[3]/button")
if len(select_plans) > 0:
select_plans[0].click()
# using find_element
select_plan = browser.find_element_by_xpath(".//*[#id='PostAdMainForm']/div[1]/div/div/div/div/div[1]/div[3]/button")
if select_plan:
select_plan.click()
Though, the link for the page you shared did not have the blue button. However, I have found it, after navigating to 'Post Your Ad' page. You can click on the Select button which is in blue color using the text appearing before it. For example, using text Basic, you can reach to the Select button. Following code shows, how we can achieve this:
select_plan = browser.find_element_by_xpath("//h3[text()='Basic']/following::button[text()='Select'][1]")
select_plan.click()
Let me know, whether it works for you.
So, currently I'm using Python 3 and the selenium webdriver with Salesforce to automate admin verifications.
I've been pretty successful (even though I'm not that proficient with programming). However, I've run into an issue... I can't seem to figure out how to find an element on the page so that I can verify the text contained within is accurately being displayed.
This is what it looks like on the user's end:
The highlighted element displays as this
But whenever I search for "GlobalHeaderCommunitySwitcher", it spits back an error that it can't find it.
So I try searching for the other elements in the block of code:
<b class="zen-selectArrow"></b>PVT GBI Internal
<b class="zen-selectArrow"></b>
"PVT GBI Internal"
I've come up empty each time by searching by:
browser.find_element_by_id("globalHeaderCommunitySwitcher")
browser.find_element_by_class_name & used "zen-trigger" and "zen-selectArrow"
browser.find_element_by_xpath("//div[#class='zen-trigger'and text()='PVT GBI Internal']")
This also results in nothing being returned..
Essentially, how do I locate the element in the screenshot via the above code and then have the script verify that the text within that element ("PVT GBI INTERNAL") is present and correct?
you can use //tag[text()="value"] or //tag[contains(attribute,‘value’)]
example : browser.find_element_by_xpath("//a[#class='zen-trigger']//*[text()='PVT GBI Internal']")
//a[#class='zen-trigger']//*[contains(text(),'PVT GBI Internal')]
//a[#class='zen-trigger']//*[contains(#class="zen-selectArrow")and
contains(text(),'PVT GBI Internal')]
Open the page using the google chrome browser
Move the mouse over the element that you want to find and right-click it
Left click Inspect (at the bottom of the selection list)
Your element will be hi-lighted in the Developers tools
Right click the hi-lighted element and select Copy
Click either copy Selector or XPath depending on your preference
Paste that into your selenium find_element_by_xpath() or find_element_by_css_selector() statement as appropriate.
Say xpath
element = browser.find_element_by_xpath("your pasted xpath")
assert element.text == 'Your expected text'
I am trying to click a small button, which has no "ID" or "Name", on a website. The only unique identifier is onclick, which is as follows:
onclick="submitForm('DefaultFormName',1,{'param1':'HXCTIMECARDACTIVITIESPAGEXgm7J5oT','serverValidate':'1uCdqvhJe','param2':'',event:'details|1'});return false;"
However, another button on the page has the following onclick:
onclick="submitForm('DefaultFormName',1,{'param1':'HXCTIMECARDACTIVITIESPAGEXgm7J5oT','serverValidate':'1uCdqvhJe','param2':'',event:'details|2'});return false;"
The only difference is a 1 vs. a 2 in the middle of the code. I tried to use a "find_element_by_css_selector" with the following code but it didn't work:
driver.find_element_by_css_selector(".x1p[onclick*='details|1']").click()
Another option would be selecting the preceding element, with the following code:
precedingbutton = driver.find_element_by_name('B22_1_6')
And then sending tab and then enter. However, after I send Tab, I don't know of a way to send Enter without assigning the Send_Keys command back to the preceding box, which deselects the button I want.
Let me know if you can help!
If you can locate the parent, you can locate the child, either with xpath or the nth-child css selector, relative to it.
Edit in response to comments:
Assuming by "2 elements away" you mean it is a sibling preceding the target, try something like td[name="B22_1_6"] + td + td. This is the sibling selector.
You can do it easily by
driver.find_element(By.XPATH, 'your xpath').click()
For finding the xpath , just inspect the button with firebug, and in html tab right click on selected element and click on Copy XPath.
Hope it will help you.