I am writing a code that will hopefully be able to click on a defined button on a pop-up window. The pop-up window appears only after having clicked somewhere else (this part is working).
here's the code:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 2)
wait.until(EC.element_to_be_clickable((By.XPATH, '//button\[text()="OK"\]')))
wait.until(ExpectedConditions.visibilityOfElementLocated(By.XPATH, '//button\[text()="OK"\]'))
wait.until(ExpectedConditions.elementToBeClickable(By.XPATH, '//button\[text()="OK"\]'))
driver.findElement(By.XPATH, '//button\[text()="OK"\]').click()
I am getting an error "TimeoutException". Anyone able to fix that?
thanks a lot
Related
Hi can someone help me how can i accept the alert after I click the save button ?
I tried the accept.alert() but its not working
Try this with explicitly wait condition:
alert = WebDriverWait(driver, 30).until(
EC.alert_is_present())
alert.accept()
imports:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
I have this piece of code which works as it is:
time.sleep(0.7)
self.driver.find_element_by_xpath("anyXpath").click()
I make the program to wait for a time so the button on the pop-up window is really there to be clicked, and this solution works.
Now, I want to do the same but with WEbDriverWait and I wrote this:
WebDriverWait(self.driver,10).until(expected_conditions.presence_of_element_located(By.XPATH, "anyXpath"))
self.driver.find_element_by_xpath("anyXpath").click()
But the button is never clicked. What am I doing wrong? I am open to use another expected condition different from presence_of_element_located but I guess it should work with the latter also.
Thanks.
By importing WebDriverWait, expected_conditions, and By, you can wait until the WebDriver deems an element clickable before it clicks the element.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
WebDriverWait(10, self.driver).until(
expected_conditions.element_to_be_clickable(
(By.XPATH, "//xpath//to//element")
)
).click()
Make sure that you pass a tuple to element_to_be_clickable().
You can try this method. It wait for element to be click able
WebDriverWait(10, driver).until(EC.element_to_be_clickable(By.XPATH, "anyXpath"))
import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Python/coding newb, beware!
Im writing a script to download from youtube from " https://ytmp3.cc/en13/ ". I had written a click script using pyautogui but the problem being that the download button appears anywhere betweeen 1 and 15 seconds of entering the link. So i wanted to re-code it in a Selenium window to dynamically wait until the button is visible and then continue with the click script. I have tried about 15 ways but cant get it to work
library blah :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.expected_conditions import presence_of_element_located
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
ways i tried to get it to work:
without waits, results in NosuchElement Exception
def check_exists():
try:
while True:
a=driver.find_element(By.LINK_TEXT, "Download")
a.click()
except NoSuchElementException:
time.sleep(10)
the syntax of this is most probably wrong
i tried a couple different variations of implicit and explicit waits but couldnt get it to work. Is there some javascript on the site linked above thats messing with my code?
in a separate .py, running
#driver.find_element(By.LINK_TEXT, "Download").click()
#either the line above or below works at finding the element, and clicking it
driver.find_element_by_link_text("Download").click()
I just need help getting the line above ^ into an explicit wait, but i dont have the language knowledge of how to do it yet. i would appreciate some help, thanks in advance!
You can use webdriver wait to wait until button is displayed. Tried the below code which works for me
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
video_path = "your_video_path"
waitTime=10
driver = webdriver.Chrome("./chromedriver.exe")
driver.get("https://ytmp3.cc/")
element = driver.find_element_by_id("input")
element.send_keys(video_path)
driver.find_element_by_id("submit").click()
try:
# wait 10 seconds before looking for element
element = WebDriverWait(driver, waitTime).until(
EC.presence_of_element_located((By.LINK_TEXT,"Download"))
)
driver.find_element_by_link_text("Download").click()
finally:
print(driver.title)
You can update your required wait time in waitTime variable in seconds so if the value is 10 selenium will wait and check for elements upto 10 seconds
Path here
I need to press the button but cant really figure out how
I tried copying the xpath but get an error
also tried to press the button over the id or class_name but also not worked
If you have an idea please write it in the comments. Thanks
Tried so far:
driver.find_element_by_xpath("//button[#class='action-button']").click()
driver.find_element_by_class_name("action-button").click()
driver.find_element_by_xpath("[#id='clearBrowsingDataConfirm']").click()
driver.find_element_by_id('[#id="clearBrowsingDataConfirm"]').click()
Should work if it's not in a iframe.
driver.find_element_by_id('clearBrowsingDataConfirm').click()
Try this if it's after driver.get()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'clearBrowsingDataConfirm'))).click()
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I tried to close a cookie button on whoscored.com, I located the button, however when the program clicks, it seems to click the ad behind the button, and it ends up opening a new page instead of closing the cookie button. Any idea of what I can do?
url='https://www.whoscored.com/Search/?t=Crystal+Palace'
browse=webdriver.Chrome()
browse.get(url)
time.sleep(4)
cacheButton=browse.find_elements_by_xpath('//button')
cacheButton[1].click() #This is the "I ACCEPT" button.
Here is the html for the button:
<button class="qc-cmp-button" onclick="window.__cmpui("setAndSaveAllConsent",!0)"> I accept </button>
Would appreciate if anyone can help me.
Induce WebDriverWait() and wait for element_to_be_clickable() and following xpath.
WebDriverWait(browse,15).until(EC.element_to_be_clickable((By.XPATH,"//button[#class='qc-cmp-button' and contains(.,'I accept')]"))).click()
Add following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
url='https://www.whoscored.com/Search/?t=Crystal+Palace'
browse=webdriver.Chrome()
browse.get(url)
WebDriverWait(browse,15).until(EC.element_to_be_clickable((By.XPATH,"//button[#class='qc-cmp-button' and contains(.,'I accept')]"))).click()
Browser sanpshot:
Update:
cookie=btn=WebDriverWait(browse,15).until(EC.presence_of_element_located((By.XPATH,"//button[#class='qc-cmp-button' and contains(.,'I accept')]")))
browse.execute_script("arguments[0].click();", cookie)