I need to drag this scale and when I run this code:
HandScale = browser.find_element_by_xpath('//*[#data-xform="scale"]')
GridLineX = browser.find_element_by_class_name('outlined')
bottomLeft = browser.find_element_by_class_name('bottomLeft')
print GridLineX.size
action_chains = ActionChains(browser)
action_chains.drag_and_drop_by_offset(HandScale, 30, 30).click_and_hold(HandScale).perform()
It still has the grabbing effect Shown here:
Is there anyway to remove this effect before running the other part of my script?
I think you just need the release() function in action_chains which is designed to do exactly that. The line to add at the end of your current file would be:
action_chains.release().perform()
Edit after feedback that this didn't work: what if you do the release() inside your existing action_chains, and simply add a pause() after your click_and_hold(HandScale) so that the click is actually held and not immediately released. Finally, since you use a webelement argument in click_and_hold(HandScale) I would try to release on that element with release(HandScale). So, your action_chains might do what you want if you use the following:
action_chains.drag_and_drop_by_offset(HandScale, 30, 30).click_and_hold(HandScale).pause(5).release(HandScale).perform()
If THAT doesn't work, the WebDriver API has a method called reset_actions() that, according to the documentation "Clears actions that are already stored locally and on the remote end". I would add this under your current action_chains, but you could try inserting it before perform() in your current code. The line to add would just look like this:
action_chains.reset_actions().perform()
If none of this works for you, you could try
driver.execute_script("arguments[0].removeAttribute('cursor')", element")
If you need I think can help you figure out what to execute if none of the above works.
Related
I am new to python, and I am trying to write a script to verify a check-box has been checked/enabled. My problem is that I am not able to read the correct value after the check box has been checked/selected or un-checked/un-selected.
I was able to locate the check box, select/enable it, then click "apply" to confirm the change. But when I tried to verify the check box's status, I could not get the correct value.
If I used .is_enabled(), the read value would always be "True" no matter what the check box has been selected/enabled or not.
And If I used .is_selected(), the value would always be "False".
Please help. Below is my code:
Locate_Checkbox = Browser.find_element(By.XPATH,'//*[#id="at-contain"]/sd-canvas-home/div/span/div[3]/sd-canvas/div/div/at-checkbox[11]/div/div')
Ranging = Locate_Checkbox.is_selected()
print(Ranging)
I really appreciate your help.
is_enabled() is used to check whether element is connected and enable to perform the operation in the current context of the page. And NOT to check whether it is selected or not.
is_selected() is used to check whether the element is selected or not. This method is used on checkboxes, input boxes, radio buttons.
In your case you should rely on the is_selected() However if you use this function with find_element() it will never reach to the is_selected() method if in case of you have exception in finding the element. Probably that is what is happening in your case. To solve this problem you will have to use find_element**s**() variant.
This will return you empty list incase of exception in finding element. for example.
locate_Checkbox = Browser.find_elements(By.XPATH,'//*[#id="at-contain"]/sd-canvas-home/div/span/div[3]/sd-canvas/div/div/at-checkbox[11]/div/div')
if len(locate_Checkbox) > 0:
Ranging = locate_Checkbox[0].is_selected()
print(Ranging)
Thank you all especially Justin for helping me on this question. I really appreciate it.
I tried the below command and it works perfectly.
Locate_Checkbox = Browser.find_element(By.XPATH,'//*[#id="at-contain"]/sd-canvas-home/div/span/div[3]/sd-canvas/div/div/at-checkbox[11]/div/div').get_attribute("class")
Once again, thank you all.
because it is div not input with type checkbox, so is_selected is not feasible for your case. you can try as the following: Locate_Checkbox = Browser.find_element(By.XPATH,'//*[#id="at-contain"]/sd-canvas-home/div/span/div[3]/sd-canvas/div/div/at-checkbox[11]/div/div').getAttribute("class"); based on class attribute to know whether the div is checked or not, seems when checked, the class is "at-checkbox at-checked", it not check, suppose it is "at-checkbox", you can try to see that –
I am trying to have pyautogui move the mouse whenever it detects a color but for some reason whenever I try running it keeps on prompting this error, I have run this code before and it worked perfectly fine. pls help
Code
Output
You are getting that error because "locateAllOnScreen" returns a "generator" which can be looped through which contains all instances of the image. You may be looking for "locateOnScreen".
Here are some example on how to use the two different functions:
# Will loop through all instances of 'img.png'
for pos in pyautogui.locateAllOnScreen('img.png')
# Code here...
# Will find one instance and return the position
pyautogui.locateOnScreen('img.png')
This link has some good information on the different methods
I'm looking for a function that makes it easier to switch between two frames. Right now, every time I need to switch between frames, I'm doing this by the following code:
driver.switch_to.frame(driver.find_element_by_css_selector("frame[name='nav']"))
driver.switch_to.frame(driver.find_element_by_css_selector("frame[name='content']"))
My goal is to get a function that takes an argument just to change nav or content since the rest is basically the same.
What I've already tried is:
def frame_switch(content_or_nav):
x = str(frame[name=str(content_or_nav)] #"frame[name='content_or_nav']"
driver.switch_to.frame(driver.find_element_by_css_selector(x))
But it gives me an error
x = str(frame[name=str(content_or_nav)]
^
SyntaxError: invalid syntax
The way this is written, it's trying to parse CSS code as Python code. You don't want that.
This function is suitable:
def frame_switch(css_selector):
driver.switch_to.frame(driver.find_element_by_css_selector(css_selector))
If you are just trying to switch to the frame based on the name attribute, then you can use this:
def frame_switch(name):
driver.switch_to.frame(driver.find_element_by_name(name))
To switch back to the main window, you can use
driver.switch_to.default_content()
I am running some browser tests with splinter and, at one point, come across a page with a link I want to follow. This call succeeds and returns the link:
my_browser.find_link_by_partial_href('/mystuff/' + str(important_number))
But I cannot click it:
my_browser.find_link_by_partial_href('/mystuff/' + str(important_number)).click()
...
...
...
ElementNotVisibleException: Message: u'{"errorMessage":"Element is not currently visible and may not be manipulated","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:38495","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\\"sessionId\\": \\"7812e810-9100-11e4-881c-37067349397d\\", \\"id\\": \\":wdc:1420039695427\\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/7812e810-9100-11e4-881c-37067349397d/element/%3Awdc%3A1420039695427/click"}}' ; Screenshot: available via screen
What's odd here is that the link is indeed present when I follow my_browser.url, as well as if I look at my_browser.html or trybrowser.show_screenshot(my_browser).
And it doesn't seem to be an issue of waiting for visibility. Adding a quick import time(); time.wait(5); before the click still doesn't work (nor do longer waits, though that's probably more than sufficient).
What could I be missing here?
Ah. Splinter is defaulting to the first link it finds, which isn't visible:
(Pdb) [link.visible for link in my_browser.find_link_by_partial_href('/mystuff/' + str(important_number))]
[False, True]
This extra hidden link isn't supposed to be there in the first place, which goes to show you what can happen if you make assumptions about your code - even the seemingly irrelevant parts!
Asking around a different way of doing this.
I am trying to look at the screen, decide if an image exists (from a list) and choose any that are there, then click "go". If none of the list items are there, it would execute click("go.png") and carry on. If there are any list items there, it clicks it and then executes the click("go.png")
wait("start.png", 10)
click("start.png")
class gameOne():
def pickone():
imageFile = ["one.png", "two.png", "three.png"]
if exists(imageFile):
click(imageFile)
click("go.png")
With this, the click("start.png") and click("go.png") work. It seems to just skips the class. No error is given.
You aren't using the class correctly, I'm not sure how you are expecting this to work, but you don't need that for what you're doing:
wait("start.png", 10)
click("start.png")
imageFile = ["one.png", "two.png", "three.png"]
if exists(imageFile):
click(imageFile)
click("go.png")
This should work as intended.