So I'm trying to scrape some information from a website and can't get through a pop-up window. I've tried using short and full Xpath of the X button but it doesn't close.
here is my code
# import
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path = 'mypath/chromedriver.exe')
driver.get('https://ai.fmcsa.dot.gov/SMS')
driver.find_Element_By_xpath('//*[#id="simplemodal-container"]/a').click();
The code does open the website but doesn't close the pop-up. What might be the issue?
You automation script needs an explicit waits, and the below xpath :-
//a[#title='Close']
Code : -
driver = webdriver.Chrome(executable_path = 'mypath/chromedriver.exe')
driver.maximize_window()
#driver.implicitly_wait(50)
driver.get("https://ai.fmcsa.dot.gov/SMS")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[#title='Close']"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
If you want to use your code as is:
# import
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path = 'mypath/chromedriver.exe')
driver.get('https://ai.fmcsa.dot.gov/SMS')
driver.find_Element_By_xpath('/html[1]/body[1]/div[7]/a[1]').click();
This worked for me and closed the pop up window.
Related
I am struggling with Selenium
for the url: https://pubchem.ncbi.nlm.nih.gov/compound/2078
I am trying to click the button Download, but it doesn't find the element.
My code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from ipykernel import kernelapp as app
import time
options = webdriver.ChromeOptions()
driver_path = 'C:\\Users\\Idener\\Downloads\\chromedriver_win32\\chromedriver.exe'
driver = webdriver.Chrome(driver_path, options=options)
url = f"https://pubchem.ncbi.nlm.nih.gov/compound/2078"
driver.get(url)
driver.find_element_by_xpath("//*[#id='"'page-download-btn'"']").click()
enter image description here
Your XPath is not valid. You don't need so much quotes
driver.find_element_by_xpath("//*[#id='page-download-btn']").click()
You are missing a delay.
Element should clicked only when it is completely rendered and ready to accept a click event. WebDriverWait expected_conditions explicit waits should be used for that.
Also, no need to add f before URL value and '"' instead of ' in XPath expression.
The following code will work for you:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from ipykernel import kernelapp as app
import time
options = webdriver.ChromeOptions()
driver_path = 'C:\\Users\\Idener\\Downloads\\chromedriver_win32\\chromedriver.exe'
driver = webdriver.Chrome(driver_path, options=options)
url = "https://pubchem.ncbi.nlm.nih.gov/compound/2078"
driver.get(url)
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "page-download-btn"))).click()
On website there is a drop-down menu "Best Time to Contact" and I click on it but I can't choose from the d-d menu. Suggestions?
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver=webdriver.Chrome(executable_path="D:\ChromeDriverExtracted\chromedriver")
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?1537702596407")
select = Select(driver.find_element_by_id("RESULT_RadioButton-9").click())
select.select_by_visible_text("Morning").click()
I ran the below code
driver.maximize_window()
wait = WebDriverWait(driver, 30)
select = Select(wait.until(EC.visibility_of_element_located((By.ID, "RESULT_RadioButton-9"))))
select.select_by_visible_text('Evening')
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
and it did the trick. This is with explicit waits, which is recommended in Selenium automation.
You should not use .click() here
select = Select(driver.find_element_by_id("RESULT_RadioButton-9").click())
Also, I tested your code without .click(), it works as well.
select = Select(driver.find_element_by_id("RESULT_RadioButton-9"))
select.select_by_visible_text("Morning")
This worked for me
driver.find_element(By.CSS_SELECTOR, "#RESULT_RadioButton-9 > option:nth-child(2)").click()
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.remote import webelement
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time
url = "https://www.mrporter.com/en-gb/mens/product/nike/shoes/low-top-sneakers/space-hippie-04-recycled-stretch-knit-sneakers/19971654707345242"
PATH = 'C:\Program Files (x86)\chromedriver.exe'
browser = webdriver.Chrome(PATH)
browser.get(url)
element_dropdown = browser.find_element_by_class_name("CombinedSelect11__field CombinedSelect11__field--selectableOption CombinedSelect11__field--nativeSelect")
select = Select(element_dropdown)
try:
select.select_by_visible_text("8")
except NoSuchElementException:
print("the item doesnt exist")
I am trying to locate the dropdown menu of the link in my code. Once the dropdown box is located I want to search by visible text for a size 8. However whatever I try it still doesn't work.
You can try using explicit wait and then perform your operation. Please take a look at the below code which I have written to replicate your scenario. It's working fine for me. Do let me know if you face any problems.
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options)
driver.get(
'https://www.mrporter.com/en-gb/mens/product/nike/shoes/low-top-sneakers/space-hippie-04-recycled-stretch-knit-'
'sneakers/19971654707345242')
wait = WebDriverWait(driver, 30)
wait.until(EC.visibility_of_element_located((By.XPATH, '//div[text()="Select a size"]'))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, '//li[#data-value="8"]'))).click()
This code should open flickr website and search for "s" and click search photos from suggestions.
But I get the following error:
"no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(text(),'Search photos')]"}
(Session info: chrome=90.0.4430.85)"
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://www.flickr.com/")
#webdriver.support.wait.WebDriverWait(driver, 1)
driver.find_element_by_xpath("//input[#id='search-field']").send_keys("s")
webdriver.support.wait.WebDriverWait(driver, 1)
driver.find_element_by_xpath("//span[contains(text(),'Search photos')]").click()
webdriver.support.wait.WebDriverWait(driver, 1)
I think there an wrong initializing the WebDriverWait.
And to click Search photos try using .execute_script(..)method with arguments[0].click(); argument.
Try the following code snippet:
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(executable_path="C:\\chromedriver.exe")
driver.get("https://www.flickr.com/")
wait = WebDriverWait(driver, 20)
search_field = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#id='search-field']")))
search_field.send_keys("s")
search_photos = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Search photos')]")))
driver.execute_script("arguments[0].click();", search_photos)
Recently i have began with Selenium in Python with Chromium Webdriver, but it doesn't fill in the Login Screen, what did i wrong? I think it has to be with the "find_element" function. Here's my Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
driver.get("https://instagram.com")
print(driver.title)
input_username = driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input")
input_username.send_keys("lowity")
time.sleep(5)
input_paswd = driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[3]/div/label/input")
input_paswd.send_keys("password")
time.sleep(5)
button_login = driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[4]/button")
button_login.click()
time.sleep(5)
time.sleep(10)
driver.quit()
And the console has no error output.
You have to wait for element being clickable in order to click it. The best way is to user the WebdriverWait() function with until(EC.element_to_be_clickable) Here is the updated code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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(executable_path = 'C:\\chromedriver.exe')
driver.get("https://instagram.com")
print(driver.title)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input"))).send_keys("lowity")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[3]/div/label/input"))).send_keys("janosch2005")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[4]/button"))).click()
driver.quit()