Cannot execute if else statement code when checking button is enabled - python-3.x

I'm trying to check the button on indeed.com after you click apply job, but my code doesn't seem to work.
The first if to check if the continue button is enabled if not, then it will close the page, switch to the main window and apply for the next job. The if else inside the if statement to check if there another continue button, it will close the page and switch to main window if not click on apply button to finish and close the page then switch to main window.
Sorry, I cannot post pictures yet so I have to link them.
Here's the picture after you hit apply job
https://imgur.com/FzITw8q
Here's the picture if there another continue button
https://imgur.com/iJMFqp8
Here's the picture if there's no continue button and only apply button https://imgur.com/62KMUwY
And the last one https://imgur.com/ls4mwEi
# Click on continue button if there any
if WebDriverWait(self.driver, 30).until(EC.presence_of_element_located((By.XPATH,'//*[#id="form-action-continue"]'))).is_enabled():
WebDriverWait(self.driver, 30).until(EC.presence_of_element_located((By.XPATH,'//*[#id="form-action-continue"]'))).click()
if WebDriverWait(self.driver, 30).until(EC.presence_of_element_located((By.XPATH,'//*[#id="form-action-continue"]'))).is_enabled():
self.driver.close()
self.driver.switch_to.window(main)
else:
WebDriverWait(self.driver, 30).until(EC.presence_of_element_located((By.XPATH,'//*[#id="form-action-submit"]'))).click()
self.driver.close()
self.driver.switch_to.window(main)
#If no button close the window and switch to main window
else:
self.driver.close()
self.driver.switch_to.window(main)
I also tried is_displayed() but it doesn't work at all.

First, I am wondering how you are calling isEnabled() on WebDriverWait. This will just wait for an element until it is located and won't return its WebElement. Hence, when you apply is enabled in line 1, 2 and 3, it throws an error.
First assign that element to one variable, and call that variable in WebDriverWait. Once element is located/ waited until element located by WebDriverWait, use click or is enabled methods using variable instead of WebDriverWait.

Related

How to verify if a button from a page is really disabled in a chromedriver window on Python? Selenium related

I'm trying to automate a process on this page, and after several attempts, I still have not figured out why the code below print True when checking if the first_wallet_option button is enabled, here:
driver.get('https://opensea.io/') #go to the opensea main page.
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="__next"]/div/div[1]/nav/ul/div[2]/li/button'))) #wait for the wallet button to be enabled for clicking
wallet_button = driver.find_element(By.XPATH, '//*[#id="__next"]/div/div[1]/nav/ul/div[2]/li/button')
wallet_button.click() #click that wallet button
first_wallet_option = driver.find_element(By.XPATH, "/html/body/div[1]/div/aside[2]/div[2]/div/div[2]/ul/li[1]/button")
print(first_wallet_option.is_enabled())
What happens is that after the page has been loaded, the program clicks the wallet icon located at the top right corner of this page, and according to its html source, those buttons are clearly disabled at the moment it is done, as shown down below:
But, if I click twice the same wallet icon (one to close the aside element, and other to open it again), they get enabled:
So, I would like to learn how could I improve the code above to make sure it really detects when all of those 4 buttons (Metamask and the other 3) are enabled.
Ok, here it is:
First: The locators are not relative enough, so I took the freedom to refactor them.
Second: MetaMask is not a button as per DOM DOM Snapshot but the rest are. However, instead of relying on the buttons, I tried to rely on the li (which is common for all), and checking whether they are enabled or not.
Third: If you are trying to find if all the 4 elements are enabled, then you must use find_elements instead of find_element and then loop through the elements to check if each is enabled.
Please check if this works for you:
driver.get('https://opensea.io/') #go to the opensea main page.
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, "//*[#title='Wallet']"))) #wait for the wallet button to be enabled for clicking
wallet_button = driver.find_element(By.XPATH, "//*[#title='Wallet']")
wallet_button.click() #click that wallet button
wallet_options = driver.find_elements(By.XPATH, "//*[#data-testid='WalletSidebar--body']//li")
for wallet in wallet_options:
if wallet.is_enabled():
print(f"{wallet.text} is enabled")
else:
print(f"f{wallet.text} is not enabled")

Selenium/ python performing a click with browser.execute_script VS. normal click line

I wanted to click on something in a webpage so I used
WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#styleguide-v2 > div.banner-container > a:nth-child(2)")))
except that it doesn't work in the background.I have to switch to the browser manually where it to be seen on my screen so that the code works properly.
Then I added this
x = WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#styleguide-v2 > div.banner-container > a:nth-child(2)")))
browser.execute_script("arguments[0].click();", x)
now it works like charm, my question is what's the difference? I want to know what's happening behind this
the webpage https://www.imdb.com/title/tt0198781/
presence_of_element_located expected condition finishes and the program continues to the next call while the element already created but still not clickable and still not located on it's final position on the page and still not ready to accept regular click.
JavaScript click can handle this kind of click, however this doesn't really imitates real UI user action.
To mimic real user action you should use element_to_be_clickable expected condition and click the element only when it became clickable.
visibility_of_element_located didn't work because the element is not actually visible itself, so we had to use element_to_be_clickable expected condition.
It is also possible that element is covered by some other element during the page rendering when it is literally become clickable but the page is still rendered. In this case we have to add some hardcoded delay or to wait until the element covering the desired button is disappeared. this can be achieved by invisibility_of_element_located expected condition for the covering element.

how do I identify if a popup comes up

I'm making a sneaker bot that simulates a checkout on shoe palace (using air force 1s as an example in this program). Basically what I'm having trouble with is after about 5 seconds after opening the website (it's not a consistent amount of time) I get a popup basically just advertising signing up for their club or whatever and it screws up my code because the button doesn't get pressed so the script terminates. I've tried using time.sleep(5) to hopefully wait for it to popup and then simulating pressing the escape key to close it but it just doesn't work. Is there a way that I can recognize when the popup comes up so I can run a piece of code that closes it immediately? Below I have the code I'm using currently which in an instance where the popup doesn't come up works perfectly. Any help is GREATLY appreciated as I've been stumped with this problem.
from selenium import webdriver
import time
# from pynput.keyboard import Key, Controller
# keyboard = Controller()
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
#driver.delete_all_cookies()
#open product page (CHANGE THIS)
#driver.get('https://www.shoepalace.com/product/nike/ck7214-100/air-force-1-07-lv8-2-mens-lifestyle-shoe-white-white/')
driver.get('https://www.shoepalace.com/product/nike/ck7214-100/air-force-1-07-lv8-2-mens-lifestyle-shoe-white-white/')
time.sleep(.025)
#secondary size
button = driver.find_element_by_xpath("/html/body/div[2]/div[4]/form/div[1]/button[5]")
button.click()
#primary size
button = driver.find_element_by_xpath("/html/body/div[2]/div[4]/form/div[1]/button[7]")
button.click()
#add to cart
button = driver.find_element_by_xpath("/html/body/div[2]/div[4]/form/div[2]/input")
button.click()
#checkout
button = driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div/div/div/ul[2]/li[1]")
button.click()
You can create a method where you should:
Identify if the pop-up is displayed
Close it if so
Wait until pop-up is not displayed
Keep in mind that your code runs line by line, so if you know when the pop up can appear, you can add your method there. If not, based on your code structure is a tricky one because you will need to add it before each action.

Python selenium cannot click or change value in radio button

Trying to automate amazon affiliate report generation. cannot change the value of a checkbox
need to mark this checkbox
this is HTML of that checkbok
radio = driver.find_element_by_xpath(
"//input[#value='custom' and #name='ac-daterange-radio-report-download-timeInterval']")
driver.execute_script("arguments[0].click();", radio)
# driver.execute_script(
# "arguments[0].setAttribute('checked', 'checked';", radio)
If more clarity needed can ask here.
thanks in advance
You are not sending click event to java script executor, instead of this you directly click by .click() method. If you want to click one of the checkbox you use one of the following commands, change the text contains: Today, Yesterday and other. See this:
driver.find_element_by_xpath("//span[contains(text(), 'Today')]/ancestor::label/input").click()
driver.find_element_by_xpath("//span[contains(text(), 'Yesterday')]/ancestor::label/input").click()
driver.find_element_by_xpath("//span[contains(text(), 'Custom Date Range')]/ancestor::label/input").click()

Click update after dialog appears Robotframework

I'm testing a scenario where I click on a button and below dialog appears. It's only possible to click on 'Update'
I have put following in my test scenario:
Click Element jquery=a.newOrder
Click Element Link=Update
I receive following error message after running the test:
Element is not clickable at point (1023, 127.19999694824219). Other element would receive the click:
The only HTML code that is related to the Update is
Update
I also try to test this with alert but no alert is found...
Can someone explain me what I need to do?
Thanks!
Explaining your scenario :
When you click update, a pop will be shown. You have to add a Wait until page contains element in any of the elements in pop up and then wait for the element you need to click. After the element is visible and focus is on the element. Click the element. If you get error in this, try to add selenium timeout for command execution speed or add a sleep to check whether the element loads slowly.

Resources