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?
Related
I am having some issues with Selenium not clicking the pop-up privacy button on https://www.transfermarkt.com/
Here is my code so far:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
It comes up saying:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div[2]/div[3]/div[2]/button"}
Anyone have any thoughts?
I believe the issue is that the element you are trying to click is inside an iframe. In order to click that element you'll have to first switch to that frame. I noticed the iframe has title="SP Consent Message" so I'll use a CSS Selector to identify it based on that. Your code with the added line:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
driver.switch_to.frame(driver.find_element_by_css_selector('iframe[title="SP Consent Message"]'))
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
Note that you may have to switch back to the default frame to continue your test, I'm not 100% sure as that iframe has gone away.
Also it seems like some folks don't get that popup when the hit the website, not sure why, may be something you want to look in to to determine how you want to test this.
Like #Prophet says you should improve the xpath for the button (again it seems to have a unique title so I would use CSS Selector 'button[title="ACCEPT ALL"]'), but this works for me to click it.
There are 2 issues here:
You need to add wait / delay before accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button') to let the page load
It's very, very bad practice to use absolute XPaths. You have to define an unique, short and clear related XPath expression.
Additionally, I see no pop-up privacy button when I open that page. So, maybe that element is indeed not there.
I am trying to take a screenshot of an element with a certain id using appium python. I want the element to be on the top of the screen so that I can grab the maximum content of that element. Currently, it takes a screenshot even if the element is at the very bottom, because of which I dont get to see the content.
It's quite confusing about your question, but from what I understand, you need to scroll the page so that the element is on top of the screen?
If so, try executing this script to your element, the element should be at top of your page now.
elem = driver.find_element_by_name("q")
driver.execute_script("arguments[0].scrollIntoView();", elem)
You can also try move_to_element then move_by_offset of page height but I don't recommend this.
If you want to take screenshot of full content of element, why not try:
elem.screenshot("foo.png")
screenshot method
I am unable to loop click the links. When I try loop click the links it keeps clicking the first link only.
From the html code, I need the element named "key" value as well. How to capture it.
html file copy in dropbox. Please click https://www.dropbox.com/sh/85rx13m8iqwax4b/AACNDq_YyOukLh22JNv76vjua?dl=0.
html code
https://pastebin.com/Cyg98W2C
Python code I tried
elem = WebDriverWait(browser, 200).until(EC.element_to_be_clickable((By.XPATH, "//DIV[#id='propertySummaryList']/DIV[#class='summaryListItem ']/DIV[1]/DIV[3]/DIV[1]/H2[1]/A[1]")))
elem.click()
browser.back()
Edit: Added dropbox link. Since the site is sign in only. I have made a copy of the page.
You can gather all the elements, then use a relative find to find the link you need. Be careful, this may cause stale elements if you don't open the click in a new window.
summaryList = driver.find_elements_by_xpath("//DIV[#id='propertySummaryList']/DIV[#class='summaryListItem ']")
for elements in summaryList:
link = elements.find_elements_by_xpath(".//h2//a")
link.text // or link.click() but need to open in a new window or will get staleElementReference
I'm attempting to make a DeviantART Llamabot for a friend as my first Selenium project with Python 3. I have the bot 99% working except for being able to find the "give llama" button.
The problem seems to be that the menu the button appears under is a popup and I can't just right click and select "copy css selector" in Firefox. As soon as I inspect the element for the give menu the menu closes and the html changes.
I've managed to take a screen shot of a random sample page of the code so I can even see what's there. I've tried learning how CSS selectors work from scratch myself and I've managed to find every nested element EXCEPT the actual items in the list. I've tried looking for Nth child and using the ">" operator. I've attempted searching by class name, name, Xpath, link name, partial link name, nothing has worked.
I've read about this problem inspecting popup elements elsewhere and the suggestions are effectively to write an HTML parser or something to copy the entire html code as it changes and then select it from your copy. I'm not going to do that for this project. It's entirely too much work unless I absolutely have to for some reason.
Honestly at this point I don't even care anymore and I just want someone to outright just tell me what to type in so I can finish this project. This is the screenshot I managed to get. I'm looking for the item highlighted in blue.
Since my code was requested for clarification here it is
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.action_chains import ActionChains
binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')
browser = webdriver.Firefox(firefox_binary=binary)
Deviant = browser.get("http://www.deviantart.com/random/deviant")
GiveMenu = browser.find_element_by_css_selector(".i47")
GiveMenu.click()
#GiveLlama = browser.find_element_by_css_selector("")`
Everything except the last line works which is why the last line is commented out until I can figure out what to put in there. No matter what I've tried, including the examples provided by the people answering this question so far, I either get a no such element error or an illegal syntax error.
You can use the xpath selector, in chrome you can use rigth click and click on copy > copy Xpath, or the element selected on your inspector is "div.popup2 .blockmenu a.f.givellama"
You should click the "Give" button first, when the pop up dialog opens, inspect the "Give a Llama Badge" element and identify the xpath. Here is a screen shot.
screen shot to locate "Give a Llama Badge" button
You can find that, the xpath to locate "Give a Llama Badge" button(Should click the Give button first to locate this element).
//a[#class='f givellama']
The xpath to locate "Give button"
//a[#href='#give-give-give']/span[text()='Give']
As you pointed out, when the popup dialog opens, if you try to test the xpath in the browser console via "Firepath" or others, when you type "Enter" or click the left mouse, the popup windows is closed. But don't worry about this, since you already identified the xpath locator, you can debug it in your scripts.
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".