JavaScript - Selenium Webdriver. How to check if element is visible - javascript

Im using Selenium to open a page. After I open the page, I want to click a some sort of refresh button. When I click it, Im getting this error:
ElementNotVisibleError: element not visible
This is the code I use to click the button:
driver.findElement(By.id(id)).click();
My guess is that the button goes not visible whenever it is pushed. And that maybe it starts off not visible as well. Cause it doesnt do a full page refresh.
Any way for selenium to check if something is visible, and maybe wait until it is?
Edit:
As it turns out, there are multiple buttons with the same ID. And the button I am trying to reach are way down. I tried to find the button with By.xpath, but I still couldnt find it. The way I did it, was to search for button by id, like this //button[contains(#id, 'abc')][1]. (Different number of course).
Selenium could not find the element, but I could find it with Chrome developer tools. Any suggestions?

Solution: 1 You can try using this code in a syso,
System.out.println("getting Page Source "+driver.getPageSource());
This method will return the entire page Source and you can check whether your button exists in the source or not. You can place the above code at several points in your function and check until you find the button in the source.
Solution: 2 In case your button does not exist in the source you will have to check and see whether the button is getting loaded in an iframe and if so you can use the following code and switch to the iframe and and then try finding the button,
driver.switchTo.frame(frameName);

In your case you could use Explicit wait to check when the element/button is clickable.
Try doing this
wait.until(ExpectedConditions.visibilityOfElementLocated(id))

Related

How to manage a button with disabled attribute using Python Selenium?

Having this button:
<button id="btn-login-5" type="button" class="m-1 btn btn-warning" disabled="">Update</button>
I want to remove the disable attribute in order to click the button.
This is the code i see everytime:
button = self.driver.find_element(
By.XPATH, "/html/body/div/div/button")
self.driver.execute_script(
'arguments[0].removeAttribute("disabled");', button)
But I can't figure me out how that can work for anyone, I mean, if the element is disabled, selenium cannot run that very first line, is not able to asign the var "button" because he can't find that element.
Am I missing something? I am getting this error if a run that:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/button"}
Update
I just realized this is related to selenium grid, I tried to click the button in a lot of ways, and no one works, but if instead of using selenium grid I use a local webdriver all of them works! So I have no idea what to do now.
If you use this relative xpath, instead of absolute one, it works:
button = driver.find_element(By.XPATH, "//button[#type='button']")
But is this the only button in your DOM? If not, you may have to add other relative entities to it.
Refactored Code (as per your query info provided):
button = driver.find_element(By.XPATH, "//button[#type='button']")
driver.execute_script('arguments[0].removeAttribute("disabled");', button)
button.click()
Output:
Process finished with exit code 0
The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the button clickable again.
You were close enough. To remove the disabled attribute, the Update element can be still identified for it's presence using the following Locator Strategy:
button = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, "//button[#class='m-1 btn btn-warning' and text()='Update']")))
self.driver.execute_script('arguments[0].removeAttribute("disabled");', button)
All works fine, the problem is a different one, my code couldn't find the element because the element was not there, something very strange, I have a docker with the page server and if I open the page I can see the button but the selenium grid webdriver doesn't load the button at all, I don't know why, maybe that button depends of some server wich is not accesible from the webdriver.
I'll ask the page developer for this, but the code was good :)
I lost a full day of work for nothing :)
If it's automated tests that fail, running with a --headless option, you could enable print-screens in case of errors and then check if the button shows or not. Might be that the button is outside of the viewport. Or the automated testing does not wait long enough for the element to be rendered.

Selenium (python). Can't find correct way to click on button

There is html where I can't find correct way to click on button via selenium on python.
Here is the part of code
So, I need to click on
class="standart-wallet_standartWalletText__zjyj3"
via selenium but it seems nothing work.
What I tried:
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='standard wallet']")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[starts-with(#class,'standart-wallet_standartWalletText')]"
wait.until(EC.element_to_be_clickable((CSS_SELECTOR, ...)))
wait.until(EC.element_to_be_clickable((CLASS_NAME, ...)))
and so on...
What I got:
raise TimeoutException(message, screen, stacktrace)
So, please help ¯_(ツ)_/¯
The element is probably hidden, as it's nested in accordion. So it won't get clickable status until getting expanded. You can try to implement a function that expands it or simply use EC.presence_of_element_located() instead.

JS: How to repeatedly reload page, read element values, then click button

Over the last few days, I've been working on a script that interacts with a particular site. A page on the site has a table of values that is randomized every reload.
I've learned that I can load the page with JQuery's $.get( ) method and then use regex to read the values of the elements, but I can't figure out how to press a button on the page.
I'm not sure how I could select an element then call the normal .click( ) method on it, or if that would even work at all.
Basically, what I want to accomplish is:
Get a new (reloaded) version of the page.
Read values from an element on the page.
Click a button on the page.
Keep repeating previous 3 steps.
How can I make this happen (with or without JQuery)?
EDIT: So it seems this is difficult/impossible to do from another page, but it works if I'm on the page. Knowing that, is there a way that I can repeating reload the page without disrupting the javascript running on it?
If the buttonpush don't trigger an get/post event, u cant do this. If the button does, you have to simulate it by sending the next get or post with the button parameters. This will get you the expected datas, so no need to refresh.

Finding if a link has visited with jquery/php

I need to find if a user has visited a link before moving on to the next page. I am trying to force them to proof a final PDF they have generated. If the link was not visited I need an alert box, if it was obviously false.
From what I can tell I will probably need both Jquery and PHP to do this. Has anyone ever ran into this issue before with results?
Why not set a global js variable with a value? If they value doesn't match when 'next' is clicked then show the alert.
Shouldn't need PHP for this.
M

Detect end of PDF document

Is there a way in Adobe Acrobat to detect if a user is on the last page? What I would like to do is set some document javascript to trigger once they are on the last page. I know how to setup the document javascript in Acrobat, just haven't found any way to detect what page its on. Ideally it would be nice to be able to get the current page and page count and compare, but even if there is a current page variable, I could just hard code the last page. My fallback will be to add a button or checkbox on the last page, but would like to avoid that. Any help would be appreciated. Thanks.
Pages have actions just like fields do, so in your case, you could use the last page's Open event to trigger your Javascript.
To set a page action, open the "Page Thumbnails" navigation pane on the left, right click the page, then add an action as you would to a field. Alternatively, a page action can be set via Javascript. In your case this would look something like this.setPageAction(this.numPages-1, "Open", action);

Categories