Click method don't open the new page. My code trials as as follows:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
drive.webdriver.Chrome(executable_path=r"C:\Users\mvintee\project_for_selenium\chromedriver_win32\chromedriver.exe")
driver.get("https://www.verio.com/")
print(driver.title)
driver.find_element(By.ID,"stylesheet1").click()
(By.ID,"stylesheet1") element represents the <body> element and ideally there is no point in clicking on the <body> element. Rather your usecase would be to click on Login
To click on the element Login you can use either of the following locator strategy:
Code block:
driver.execute("get", {'url': 'https://www.verio.com/'})
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
driver.find_element(By.CSS_SELECTOR, "div.login-form > a[href*='/secure/login.html']").click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Related
I'm trying to click on the checkbox on this website https://echa.europa.eu/information-on-chemicals . For that I use ActionChains library. At first I tried by simply getting the input tag of the checkbox by xpath or css seletor and using click() to click on it but then i got element not interactable error so I triedclick ActionChains to move over to the element and then clicking it but then i getting the following error - MoveTargetOutOfBoundsException: Message: move target out of bounds
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
actions =ActionChains(browser)
WebDriverWait(browser,5)
.until(EC.visibility_of_element_located(('xpath','//input[#id="autocompleteKeywordInput"]')))
termbox = browser.find_element(By.CSS_SELECTOR,'#disclaimerIdCheckbox')
actions.move_to_element(termbox).click(termbox).perform()
inputField = browser.find_element('xpath','//input[#id="autocompleteKeywordInput"]')
typeInput = actions.move_to_element(inputField).click(inputField).send_keys('100-09-4').perform()
WebDriverWait(browser,5)
get_url = browser.current_url
print("The current url is:"+str(get_url))```
[1]: https://i.stack.imgur.com/3Cppt.png
In case all you are looking for here is to click the checkbox, the only thing you need to fix is the locator. It is not the input but label element who should be clicked.
The below code works
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("--start-maximized")
s = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=s)
url = 'https://echa.europa.eu/information-on-chemicals'
wait = WebDriverWait(driver, 10)
driver.get(url)
wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'disclaimerIdCheckboxLabel'))).click()
To click on the checkbox you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.get('https://echa.europa.eu/information-on-chemicals')
# WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='disclaimerIdCheckbox']"))).click()
Using XPATH:
driver.get('https://echa.europa.eu/information-on-chemicals')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[#for='disclaimerIdCheckbox']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
I want to access an website with selenium and than a addblock-window appears, in which i need to click a button for it to disappear.
Eventhough I can find my XPath(//button[#title='Einverstanden'], /html/body/div/div[2]/div[3]/div[1]/button[#title = 'Einverstanden'] or
//button[contains(text(),"Einverstanden")]') in the browser,
I can't find it with my Python script. And i can't seem to find the mistake.
Here is my code.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.get("``https://www.derstandard.at/story/2000134260361/endspiel-vor-gericht-prozess-gegen-boris-becker-startet-in-london``")
driver.maximize_window()
x = driver.find_element(By.XPATH, "//button[#title = 'Einverstanden']")
print(x)
This is the error I'm getting.
The button is enclosed in an iframe in which case, you first need to switch to the iframe and then access the element
This should work:
driver.get("https://www.derstandard.at/consent/tcf/")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//div[contains(#id, 'sp_message_container')]//iframe")))
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[title='Einverstanden']"))).click()
Wait Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
In case you want to switch to default frame again, you may use this when required:
driver.switch_to.default_content()
I am making a script that will navigate through a website and perform some actions for me. But I am stuck trying to make my script click on the following elemnt:
<img src="../Images/copy.gif">
As there is no ID or class name for this element, i tried to find the element by xpath and clicking on it by using the following code:
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
....
copy = driver.find_element_by_xpath('//[#id="divGrid"]/div[2]/div[2]/table/tbody/tr[1]/td[1]/div/span/a[2]')
copy.click()
This isn't working, so I am open to suggestions to how to solve this issue.
The desired element is a JavaScript enabled element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Copy Trip']>img[src$='/Images/copy.gif']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#title='Copy Trip' and contains(#href, 'OnCopy')]/img[contains(#src, '/Images/copy.gif')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I'm trying to click on Jetzt bewerben available in this webpage using selenium. The script that I've written so far can click on that grid If I stick with hardcoded delay. I would like to do the clicking without using any hardcoded delay.
I've tried with:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "https://jobs.deloitte.de/job/D%C3%BCsseldorf-Werkstudent-%28mwd%29-Administration-Business-Process-Solutions/522320501/"
driver = webdriver.Chrome()
driver.get(url)
wait = WebDriverWait(driver,10)
time.sleep(5)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"a#bewerben_top > h1"))).click()
How can I click on that grid shaking off hardcoded delay?
There is not a problem with a fact that button is not clickable or not visible.
The problem is that there is Javascript doing some "magic" to the button.
All you need to do is to wait until the document is complete.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "https://jobs.deloitte.de/job/D%C3%BCsseldorf-Werkstudent-%28mwd%29-Administration-Business-Process-Solutions/522320501/"
driver = webdriver.Chrome()
driver.get(url)
wait = WebDriverWait(driver,10)
wait.until(lambda d: d.execute_script("return document.readyState")=='complete')
driver.find_element_by_css_selector("a#bewerben_top > h1").click()
Your locator strategy was perfect. However, there are a couple of things:
It seems the Browsing Context of the website https://jobs.deloitte.de/job/D%C3%BCsseldorf-Werkstudent-%28mwd%29-Administration-Business-Process-Solutions/522320501/ returns back the control even before document.readyState turns complete.
Ideally, you should acknowledge the Acceptance of the website's cookie policy.
As per best practices, while invoking click() you need to induce WebDriverWait for the element_to_be_clickable().
You can use the following Locator Strategies:
driver.get("https://jobs.deloitte.de/job/D%C3%BCsseldorf-Werkstudent-%28mwd%29-Administration-Business-Process-Solutions/522320501/")
WebDriverWait(driver, 10).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#cookie-acknowledge"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#bewerben_top > h1"))).click()
Note: You have to add the following imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
PS: In such one off scenarios you may require to wait for the jQuery to complete as follows:
WebDriverWait(driver, 20).until(lambda driver: driver.execute_script('return jQuery.active') == 0)
I'm just learning how to webscrape dynamically using Selenium in Python. I'm currently trying to click on a link within the webpage to page forward over search results.
So far this is the code that I'm using:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('C:\\Users\\km13\\chromedriver.exe')
driver.get("http://www.congreso.gob.pe/pley-2016-2021")
elem = driver.find_element_by_css_selector("img[src='/Sicr/TraDocEstProc/CLProLey2016.nsf/8eac1ef603908b5105256cdf006c41b1/$Body/0.AB2?OpenElement&FieldElemFormat=gif']")
elem.click()
This is the HTML that corresponds with the element I'd like to click on:
`<img src="/Sicr/TraDocEstProc/CLProLey2016.nsf/8eac1ef603908b5105256cdf006c41b1/$Body/0.AB2?OpenElement&FieldElemFormat=gif" width="81" height="16" border="0">`
From my somewhat limited knowledge of HTML this seems like the link is actually embedded in the gif which is why I tried to use the CSS selector that goes along with that image. But this did not work.
Any guidance would be greatly appreciated!
Update:
I changed my code by adding the following import
from selenium.webdriver.common.by import By
And I changed the following:
elem = driver.find_element(By.CSS_SELECTOR, "img[src='/Sicr/TraDocEstProc/CLProLey2016.nsf/8eac1ef603908b5105256cdf006c41b1/$Body/0.AB2?OpenElement&FieldElemFormat=gif']")
elem.click()
Now I get an error for "no such element."
There is an iframe.You need to switch to iframe first to access the element.Try below code.use WebDriverWait to handle dynamic element.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome('C:\\Users\\km13\\chromedriver.exe')
driver.get("http://www.congreso.gob.pe/pley-2016-2021")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.NAME, 'ventana02')))
elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
(By.XPATH, "//a[contains(#onclick,'A50')]/img[contains(#src,'Sicr/TraDocEstProc/CLProLey')]")))
elem.click()
EDITED
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome('C:\\Users\\km13\\chromedriver.exe')
driver.get("http://www.congreso.gob.pe/pley-2016-2021")
driver.switch_to.frame(0)
elem=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(#onclick,'A50')]/img[contains(#src,'Sicr/TraDocEstProc/CLProLey')]")))
elem.click()