I'm using selenium and I have a problem handling the accept cookies pop-up.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
web = webdriver.Chrome("C:/chromedriver")
web.get('https://www.spaargids.be/sparen/schuldsaldo-berekenen.html')
time.sleep(2)
akkoord = web.find_element_by_xpath('/html/body/div/div[2]/div[3]/button[1]')
When I inspect the element, I can easily find it. But selenium is unable to do it.
Does anybody know how to fix this?
The problem is that the element is inside an iframe, you need to switch to the respective iframe.
Try this before akkoord variable:
web.switch_to.frame(web.find_element_by_xpath('//iframe[#title="SP Consent Message"]'))
That should make it work, then use the same approach but updating the XPATH to switch to the iframe where the content is.
Related
The following pages,https://www.zjzwfw.gov.cn/zjservice/front/index/page.do?webId=1, I would like to locate the '城乡居民养老保险参保登记',However, this text information is not in the web source code,but I can get this element information correctly.I'm very curious about this.
The code is as follows:
from selenium import webdriver
from selenium.webdriver.common.by import By
import chromedriver_autoinstaller
from selenium.webdriver.chrome.service import Service
from xpath_helper import xh, filter
chromedriver_autoinstaller.install('/Users/project/chromedriver')
options = webdriver.ChromeOptions()
options.add_argument('--headless')
browser = webdriver.Chrome(service=Service())
browser.get("https://www.zjzwfw.gov.cn/zjservice/front/index/page.do?webId=1")
#%%
el = xh.get_element(filter.value_contains(str('城乡居民养老保险参保登记')))
html1 = browser.find_element(By.XPATH,str(el))
html1.click() ## It runs correctly.
Yes, you're right, the element is generated after rendering.
Some elements are JavaScript generated, as a result might not be readily present in the source until browser renders.
Occasionally, you might find out that the element isn't found and as a result you might have to wait until element is visible using selenium webdriverwait
By applying browser.get() method Selenium will wait for the page to be loaded according to selected Page Load Strategy. The default Page Load Strategy is normal. According to the documentation in this page loading strategy WebDriver should wait for the document readiness state to be "complete" after navigation.
So, the answer to your question is Yes, in this specific case Selenium will wait for page to be loaded.
And this is not dependent what locator are you using XPath, CSS Selector, ID or any other.
Important to understand, that if Selenium command browser.find_element(By.XPATH,str(el)) were coming after some other command, like element.click() Selenium would not wait for that element rendered until you use WebDriverWait, like wait.until(EC.element_to_be_clickable((By.ID, "the_id"))).click()
This is because the default value of driver implicit wait is 0 (documentation).
The code I want to extract the "ul" element from
After trying suggestion, I want to get "title" in the follwing "li" tag
I'm trying to extract the following "ul" element from a webpage using selenium. Using Python, I can't figure out what the X_PATH should be, I've tried everything I could think of. Also tried the css_selection. I'm getting back nothing.
I want to iterate over the "li" elements within that specific "ul" element. If anybody could help it would be appreciated, I've literally tried everything I can think/search.
//ul[#aria-label='Chat content']/li[1]/div[1]/div[1]
To me it looks like you could just grab the ul by it's aria label and then select the first li from there. You could also just inspect the element and copy the xpath from the developers console.
It also seems to be in an iframe.
wait = WebDriverWait(driver, 30)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[#class='embedded-electron-webview embedded-page-content']")))
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Check is that element are inside some iFrame, maybe is for that, if that dont work install selenium extension in your browser and try to recreat the flow, you will receive all xpath, is a way that I use when nothing is working to me.
I am trying to access PenFed in order to get my current outstanding amount. I have done quite a bit of research and unfortunately, I am still stumped. I am using Python Selenium and I am trying to click on the initial login button on the side in order to see the username field. This is the element's HTML code:
Login
When I try to run the following code:
driver.find_element_by_id("mobile-login").click()
I get the following error:
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
Even when I try to use WebDriver Wait functions such as these:
try:
WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.ID, "mobile-login"))).click()
except ElementNotVisibleException:
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, "mobile-login"))).click()
No matter how long I make them wait, I get a timeout message:
raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
All of my research says that invoking a wait function should fix it but it doesn't work for me. I also read that there might be an image overlay on top of the element that I would have to invoke before clicking on the button, but I didn't see anything in the website code as well. If I am testing it out, the only way that I am able to click on the button through code is if I physically click on it first, so I am unaware of anything else I can use. Thank you in advance for the help!
UPDATE: I have discovered that the following code works for me:
element = driver.find_element_by_id("mobile-login")
driver.execute_script("$(arguments[0]).click();", element)
But I do not know what the execute_script actually does. Can someone explain that piece of code works or if any other alternatives work for them?
The code you specified is JQuery. execute_script(p1, p2) runs a js script, where p1 is the script (in your case a JQuery line that clicks the element) and p2 is the desired element. It seems like you shouldn't need p2 if arguments[0] is equal to "element," but I'm not totally sure.
One potential fix is to use a counter for the number of times you clicked the element. If the counter reaches a certain number and the page doesn't change (you can check by finding a unique element/value on your current page), then you know it's not clickable.
Good luck!
The desired element is a dynamic element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use the following solution:
Code Block:
from selenium import webdriver
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')
options.add_argument('--disable-extensions')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://www.penfed.org/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.pfui-button.login-slide-button.pfui-button-login.dtm-global-nav[data-id='Open Log In Drawer']"))).click()
Browser Snapshot:
The link you are attempting to click is for the mobile site and is not visible if you are viewing the site at desktop resolutions. If you shrink your browser down until it changes layout, you will see the LOGIN button appear that corresponds to that link. That's why you are getting the ElementNotVisibleException.
To your second question, the reason that using .execute_script() works is that it executes JS directly and can click on anything... hidden or not. Selenium was designed to interact with the page as a user would so it won't let you click invisible elements, etc.
If you are intending for your script to act like a user, you will want to avoid using .execute_script() because it allows you to do things on the page that a user cannot do.
If you want to log in like a desktop user would, you need to click the "LOG IN" button using the CSS selector below
button[data-id='Open Log In Drawer']
That will open a side panel where you can enter your username, etc. and log in. FYI... you will likely need a wait to give the panel a chance to open before continuing the log in process.
I am currently using the following piece of code to navigate to the middle of the page but it is not working properly.
driver.execute_script("window.scrollTo(0, document.body.scrollHeight/2);")
Also, I am trying to use
element.location_once_scrolled_into_view
Can someone help ?
You may call .scrollIntoView() in your script passing in your element as an argument:
driver.execute_script("arguments[0].scrollIntoView();", element)
There is also move_to_element() built-in selenium action:
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(element).perform()
The differences were perfectly highlighted here:
scrollIntoView vs moveToElement
Having trouble getting selenium to send keys to username field. I have tried "find_element_by_css_selector" and all the other find elements. Keep getting no such element exception.
This is the code I have so far:
import webbrowser
from selenium import webdriver
browser = webdriver.Chrome()
webbrowser.open('https://www.kraken.com/en-us/login')
browser.implicitly_wait(10)
browser.find_element_by_name('username').send_keys('your-username')
Good Question.This problem is caused because Selenium didn't find the element.TO fix this you need to replace the implicity_wait with 'time.sleep(in seconds)'.This will make selenium wait for the entire website to load.Some elements of the website take more time to load.Implicity wait won't work here.