I'm trying to perform DND action, and I'm facing a problem that drives me crazy!
For some reason Selenium keeps using my cursor instead of browsers own cursor. Doesn't matter what method I use (I tried drag_and_drop and also combination of click_and_hold() + release() methods, but without any luck), the element is always dropped where my real mouse cursor is. I've seen this problem raised before (here is similar one on Java, also an issue on gitHub, another one), but unfortunately I found no solution. I've spent many hours trying to make it work and I hope someone actually has an experience of solving this problem.
def test_dnd():
driver = webdriver.Chrome()
driver.get("https://www.w3schools.com/html/html5_draganddrop.asp")
driver.find_element(By.CSS_SELECTOR, "#accept-choices").click()
src = driver.find_element(By.CSS_SELECTOR, "#drag1")
target = driver.find_element(By.CSS_SELECTOR, "#div2")
ActionChains(driver).drag_and_drop(src, target).release().perform()
Related
I'm trying to click on some element but it's not working:
driver.find_element_by_xpath("//span[text()='ENG']")
When I add:
driver.maximize_window()
before click action, it works, other codes are not working again.
I had similar problem
When I was looking for an element, it was not yet available in the code.
Fixed by adding
driver.implicitly_wait(30) ## 30 is the time he will wait
before searching for the element.
This line makes the code wait until the entire page is loaded before looking for an element.
I'm trying to click on some element via driver.find_element_by_xpath("//span[text()='ENG']"), but it's not working,
You need to post logs or be more specific. Are you getting an error on the call to find_element_by_xpath(), or when you call click as you mention below?
when I add driver.maximize_window() before click action, it works,
other codes are not working again, please advise which can be the
reason
The relevant code needs to be provided. I believe you're saying that when you call maximize_window() before you locate the element, it works, but if you don't call maximize window, it fails. This could be for a variety of reasons, but it sounds Javascript related. A similar question like this this could help. If maximize window doesn't actually help the issue, I would look into implicit waits or WebDriverWaits.
I'm trying to automate a procedure on a really old IE-only webpage and, at one point, it raises an alert.
I can't inspect it using IE (or don't know how) but there's only an "accept" button (actually an image) and the alert text can't be copied (not sure why).
I'm using selenium with the IE drivers and i can't get past this alert.
Selenium IS detecting the alert, but when i check its contents i get nothing.
I've tried accepting the alert with
alert_obj = self.br.switch_to.alert
alert_obj.accept()
and also
.dismiss();
.send_key(Keys.ENTER)
and some other things. Am i missing something?
photo of alert:
alerts are generic dialog boxes that include a text and ok button. If your alert has an image as button, then it is not an alert, but something else. I don't know what it is, but not an alert.
This is an alert:
If this popup is an alert, you can handle it while initializing driver itself. Below is C# Code for that:
InternetExplorerOptions options = new InternetExplorerOptions
{
UnhandledPromptBehavior = UnhandledPromptBehavior.Accept,
};
driver = new InternetExplorerDriver(options);
Above code should handle alert. if not, try adding this code before creating driver instance.
options.AddAdditionalCapability("browserstack.ie.enablePopups", "accept");
This question is a possible duplicate of:
How to close yet another chrome popup using python selenium
To moderators: I tried to flag this question as Duplicate, but then, when I typed "How to close yet another chrome popup using python selenium" into the search bar it found no results.
I somehow managed to do it.
I tried everything and everything was buggy. I even had to use this weird loop because just using a time.sleep() call was bugging things up. I really don't know what happened. Sometimes when entering the page the pop-up handle would appear and sometimes not. Sometimes in the wrong place. Sometimes it wouldn't close properly. I tried a pile of different ways and this one seems to work:
Here's what i did:
#Saves ID from original window
janelaOriginal = self.br.current_window_handle
#Go to the website
self.br.get(url)
#waits 2.5 seconds for the pop-up (time.sleep bugs)
i = 0
while(i < 25):
i += 1
time.sleep(0.1)
#is pop-up open?:
if(len(self.br.window_handles)>1):
#handle sometimes appears in the wrong place so this is necessary:
if(janelaOriginal==self.br.window_handles[0]):
self.br.switch_to_window(self.br.window_handles[1])
else:
self.br.switch_to_window(self.br.window_handles[0])
#close the pop-up and go back to the original window
self.br.close()
self.br.switch_to_window(janelaOriginal)
#do stuff
return
#do other stuff
I am in a project in which I am making a modular behavior driven framework for the company I am working right now. In making a modular approach of the step "user accepts alert", when I test it and came an expected alert box, it automatically closes itself and therefore shows this "NoAlertPresentException: Message: No alert is present" exception.
I have done this codes so far:
def acceptalert():
alert = driver.switch_to.alert
alert.accept()
driver.switch_to.parent_frame()
This code snippet works as I have those modules in which I incorporated the closing of alert box. The only problem is just when I try to make this one a standalone module in my framework. I have done research with this one but I really never got my problem answered. I hope there will be one in here who can help me. Thank you very much.
Found the answer. Thank you. I implemented the step "user clicks '' button" where is the value of the button being matched with a series of xpath and if the xpath returns zero match, then it fetches the available iframes and loops inside each one. When the xpath still returned zero matches, then it switches back to the main frame with this peace of code:
driver.switch_to.parent_frame()
The side effect with this one is it will dismiss the alert boxes. My problem is solved. Thanks everyone.
I'm working on a bit of automation that basically opens YouTube, plays a particular video, then opens the "Stats for Nerds" dialog and grabs the info from it. This was working yesterday. Today, I added lines to set the video to 1080p, then go into full screen, and now it fails to work.
The following line works:
driver.find_element_by_id("movie_player").click()
While this set fails, throwing a StaleElementReferenceException:
element = driver.find_element_by_id("movie_player")
actions.move_to_element(element)
actions.context_click(element)
actions.perform()
Nothing I have done can avoid this. I've tried putting it in a try block, with and without some waits thrown in. Somehow the element is going stale in the very short time between finding and setting the element, and performing the context_click action.
Is there any way of getting around this?
Edit: I'll keep checking back in to see if someone knows a better way to do this, but for now, in the interest of expediency, I've just kludged it by using pymouse to actually take control of the mouse and right click.
I suspect the DOM changes if you mouse over the button. To workaround it, "refind" the element:
element = driver.find_element_by_id("movie_player")
actions.move_to_element(element).perform()
element = driver.find_element_by_id("movie_player")
actions.context_click(element).perform()
So I have a website which has a combo that I need to select an item from, now the problem is it's a bit untraditional and doesn't have option's as elements but instead it has divs.
So I need my program to click combo box then wait (the best way to do this I found is via implicitly_wait(3)# 3 seconds) and then click the box element I need. Firefox is doing a great job with it but PhantomJS seem to throw:
Selenium.common.exceptions.ElementNotVisibleException:
Message: 'Error Message => \'Element is not currently visible and may not be manipulated\'
I'm not sure what's the cause of it, but I suspect that PhantomJS fails to correctly wait via implicitly_wait for some reason and tries to select non-visible element.
Any idea how to approach this without forced thread sleep?
Yup, your issue sounds exactly something I've just fixed in a UI test that was starting to anoy me. Quite a complex one, that passed on all browsers, except my favorite PhantomJs (which is the fastest).
It was quite anoying, when in the debugger I could clearly see that even the parent element was set to visible. Only the prime faces component I needed to click for whatever reason (not CSS or active Styles) was not visible.
After looking at Phantom JS screenshot, I realized the window was quite tiny, and indeed the UI element was not visually visible.
You see now where I am geting at?
Phantom JS is a headless browser, it does not render you the window, but it does use a window, and the window does have a size.
The exception is quite unexpected, because the engine behind is the same as the one used for chrome and safari ... things should just work.
Well, and they did in the end.
Try to tune your driver factory like this:
WebDriver driver = new PhantomJSDriver();
driver.manage().window().setSize(new Dimension(800, 600));
return driver;
Good luck.