why is selenium webdriver not finding my element - python

https://www.mykplan.com/participantsecure_net/TermsAndConditions.aspx
I am doing find by id/xpath/name and they all fail for the accept button. Here is my latest effort
driver.find_element_by_xpath('//*[#id="Accept"]').click()
copied straight from chrome web tool

The button is located inside a frame. Given xpath is correct only inside a frame. I tested xpaths in chrome console and this is what I got:
In case of main page (https://www.mykplan.com/participantsecure_net/TermsAndConditions.aspx) xpath couldn't be located:
$x('//*[#id="Accept"]');
[]
In case of frame contents only (https://www.mykplan.com/participantsecure_net/TermsAndConditionsBottom.aspx) xpath could be found:
$x('//*[#id="Accept"]');
[<input type=​"submit" name=​"Accept" value=​"I agree" id=​"Accept">​]
In selenium, I guess you need to switch to a frame before looking for xpath. I think that web driver function
driver.switch_to_frame("frameName")
should help. In your case, frame with buttons is called "bottomFrame".

Related

Changing focus to a new window in Python/Selenium

Just like the title says, I am having trouble with getting my code to focus on a new window, using the "driver.switch_to_window".
My task is to:
Click button on the parent page > new window appears > click an "accept" button on the new window and continue with the rest of the code
driver.get("https:testpage.com/")
driver.find_element_by_xpath("/html/body/div[1]/div[1]/main/div/div/div/div[2]/ul/li[1]/button").click()
#here the window appears
time.sleep(2)
driver.switch_to_window("windowName")
driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[3]/button[2]").click() #here nothing happens
You can try to get the page source so that you are sure whether the driver has switched or not:
html_source = browser.page_source
It is possible that it has switched, but your element is not loaded or is in iframe. If your element is present, then you can try with different XPath that is relative, e.g. find the nearest id and find your element from it:
//div[#id='someid']//button[text()='someText']
I do not use absolute XPaths as I think they are too fragile.
Reference:
Python Selenium accessing HTML source
What is the difference between absolute and relative xpaths? Which is preferred in Selenium automation testing?

an element is not visually searchable in chrome inspection

I am building a web scraper selenium but the element I want to click is actually searched in chrome inspection layout but can't be seen visually(like when you hit ctrl+F and search for an element by typing it in, it shows up in the DOM structure collapsing...)
So I can't proceed since I should make a "clicking" on it with my Python.
http://bitly.kr/Nypl88
is the link and the element I would like to click with selenium has an id called "cns_Tab21". When you search for it, the result total is 1 but can't be seen in the DOM.
Thank you for reading this post and for your answer in advance.
Your element is hidden inside an iframe.
To get to it you will need to switch to the iframe first:
driver.get("https://finance.naver.com/item/coinfo.nhn?code=255440");
WebElement iframe = driver.findElement(By.id("coinfo_cp"));
driver.switchTo().frame(iframe);
WebElement tabElement = driver.findElement(By.id("cns_Tab21"));
*Edit*
Added a Python implementation
driver = webdriver.Chrome()
driver.get("https://finance.naver.com/item/coinfo.nhn?code=255440")
iframe = driver.find_element_by_id("coinfo_cp")
driver.switch_to.frame(iframe)
tabElement = driver.find_element_by_id("cns_Tab21")

How Do I Use Selenium WebDriver To Click A Non Anchor Tag?

I am using Selenium WebDriver to do automated testing of a website. I have been successful in clicking through numerous menus and links to a point.
At one point the website I am working with generates links that look like this:
<U onclick="HourglassSubmitItem(document.all('PageName').value, '00000001Responsibility Code')">Responsibility Code</U>
I am trying to use the .click functionality of the webdriver to click this link with no success.
Using this:
page.find_element_by_xpath("//u[contains(text(),'Responsibility Code')]")
successfully finds the U tag above. but when I add .click() to the end of this xpath, the click is not performed. But it also does not generate an error. So, my question is can Selenium be used to simulate clicks on an HTML tag that is NOT an anchor () tag? If so, how?
I will also say that I do not have control over the page I am working with, so changing the to is not possible.
I would appreciate any guidance the Community could provide.
Thank You for you help,
Chris
Sometimes using JavaScript could solve the "clicking" issue:
element = page.find_element_by_xpath("//u[contains(text(),'Responsibility Code')]")
page.execute_script('arguments[0].click();', element)
You can prefer JavaScript in this case.
WebElment element = page.find_element_by_xpath("//u[contains(text(),'Responsibility Code')]")
JavaScriptExecutor executor = (JavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].click();", element);

Python Webdriver need code to find a particular element

I have used Python 2.7, Webdriver and Chrome to access Pinterest to insert images to a board. I have successfully logged in to the site, created a board and clicked on the Pin Image button (thanks to Stack Overflow). The problem that I have is to identify and click the “No Thanks” button using xpath find elements code. I attach an image of the web page and the Chrome inspect on the element.
Pinterest 'Popup'
Not Now Element code
I guess you can give a try to this xpath, which will grap the first element containing the class "cancelButton". Hopefully, the button on your popup will be the first element on the page containing this class.
button[contains(#class, 'cancelButton')]
hope this helps
//span[contains(text(),'Not now')]
this is general syntax: //tag[contains(attribute,‘value’)]

Clicking JavaScript links in Selenium WebDriver and Python

I am using Selenium Webdriver in Python and I got stuck trying to activate a javascript button.
What I need to do here is to click the Go to Previous Month button twice so that I have August 2014.
And then I need to click on one of the days.
The images below show the code. Please tell me if I need to provide more info.
THIS IS THE "GO TO PREVIOUS MONTH BUTTON" + INSPECT ELEMENT
AND HERE I'VE CLICKED ON THE 1ST OF AUGUST + INSPECT ELEMENT ON "1"
How do I do it?
First find your element with CSS selectors (you need to be familiar how CSS selectors work - this is prerequisite for most web development):
elem = webdriver.find_element_by_css_selector("a[title='Go to previous month']")[0]
Related WebDriver documentantion.
Then when you get your elem (there might paeg loading time, etc. issues you need to deal with) you can click it.
elem.click()
See also: Wait for page load in Selenium
Related click() documentation.

Categories

Resources