Click on Pop up - python

does anyone know how I can get past this pop-up using selenium? When I log into Facebook regularly it doesn't come up but for some reason unknown to me, it keeps firing up when I run my script.
cookie_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[id='u_0_j_I5']"))).click()
This is the script I'm trying to use to get rid of it but it isn't working

The css selector that you are using:
button[id='u_0_j_I5']
looks dynamic and brittle in nature. What that means is, every time you refresh a page you would see a different id generated from the backend.
I would suggest you use a locator which should be reliable and not too easy to break.
A CSS:
input[data-cookiebanner='accept_button']
or XPath
//input[#data-cookiebanner='accept_button']
but you should be mindful of the uniqueness of the locator.
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath/css and see, if your desired element is getting highlighted with 1/1 matching node.
If they are unique, then you can use the below code:
cookie_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[data-cookiebanner='accept_button']")))
cookie_button.click()
Update:
Use the below XPath:
//button[contains(text(),'Allow All Cookies')]
and click it like this:
cookie_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Allow All Cookies')]")))
cookie_button.click()

Related

Selenium wait for an element not present on load page

I'm trying to use Selenium to scrape google maps unfortunatly its's not quite working,the element is not present on page load and is added after clicking on some button but it seems that the element is not always loaded when looking for it. (I'm talking about the carousel's items that appears after clicking on a shop,restaurant while doing a specific search)
I already tried to use the classic Selenium wait options.
Things I tried:
time.sleep()
WebDriverWait(driver,30).until(EC.visibility_of_element_located(...)
WebDriverWait(driver,30).until(EC.element_to_be_clickable(...)
WebDriverWait(driver,60).until(EC.presence_of_all_elements_located(...)
Even with theses things results are random, I sometimes can access the element via Selenium and sometimes I don't.
It seem's that even with long waits, Selenium can't always access the web element even tho I can click it and see it.
You can make the web driver wait until the element is clickable. Have you tried that?
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "myElement")))
or a better way of doing this as pointed out here
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "myXpath")))

Selenium on Python - Submit Method Doesn't Seem to Send Request Correctly?

I'm using Selenium in a Python script to query NASA's Small Body Database for the location of asteroids. I've been working with a Python shell to check that my commands are getting the results I want, but I'm having a consistent issue with the "submit" method.
The page I'm working on is https://ssd.jpl.nasa.gov/sbwobs.cgi. All I want to do is have Selenium click on the "change" link next to "Observation Time," fill in a field on the page that opens, and then click the resulting "Use Specified Time" button.
I've set up a Selenium WebDriver object named "browser", and then the first command I send is:
browser.find_element_by_css_selector("a[href*='time']").click()
Which works and sends me to my target page, which is https://ssd.jpl.nasa.gov/sbwobs.cgi?s_time=1#top
I then find the text field and fill it in with:
browser.find_element_by_name('obs_time').send_keys("2021-04-01 01:00")
... last thing I need is to click the "Use Specified Time" button to accept this. I tried with:
browser.find_element_by_name("check_time").click()
.. but this had no response. Then I tried:
browser.find_element_by_name("check_time").submit()
... which seems to work, but takes me back to the previous page without actually changing the time. If I manually click on the button it would work fine.
Selenium is definitely finding the right element, and the submit method is doing something, but it's maybe not sending the info in the text box correctly.
I don't know anywhere near enough about HTML etc to be able to figure this out. Can anyone offer any advice?
You were close enough. Before you set the new Observation Time as 2021-04-01 01:00 you need to clear up the previous input. Ideally, to change the Observation Time you have 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://ssd.jpl.nasa.gov/sbwobs.cgi')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='time']"))).click()
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='obs_time']")))
element.click()
element.clear()
element.send_keys("2021-04-01 01:00")
driver.find_element_by_css_selector("input[name='check_time']").click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#href, 'time')]"))).click()
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='obs_time']")))
element.click()
element.clear()
element.send_keys("2021-04-01 02:00")
driver.find_element_by_xpath("//input[#name='check_time']").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:
Thanks to #DebanjanB for the comprehensive code that improved all my methods; however, I found that everything worked as expected with his code and mine by switching to Firefox.
Evidently it's some anomaly in Safari's web driver that was causing the issue.

Css or Xpath selector can't select a list element

I am trying to automate a process on a website that dynamically generates IDs for its elements :
Ids have this form:
ZCODE:FORM:j_1279323:element
I managed to make CSS or XPATH selectors for most of the elements.
I am struggling though with a ul/li element which I manage to click on with its id but not with a relative XPath, which is what I aim to achieve:
I have tried all sorts of xpath:
/html[1]/body/[1]/div[37]/div[1]/ul[1]/li[13]
also:
//div[contains(#id, 'voie_panel')]/div/ul/li[13]
And many other different ways..
All the xpath/css selector I tested work perfectly in chrome developer console.
I only manage to drop the list down, but when I am trying to access the list element... it times-out.
I am using WebDriverWait, I have also tried to Pause the program at the exact point where it has to be loaded in order to click on the list.
I wait for the element with:
myElem = WebDriverWait(self.driver, 30).until(ec.element_to_be_clickable((BY.XPATH, css))
To summarize the situation :
It works smoothly with the ID but times out with an xpath or css selector
Can someone recommend a strategy to overcome this ?
if you are using selenium web driver you can add this to your code
chrome_options = webdriver.ChromeOptions()
scpathTemp = str(scpathTemp).replace('/', '\\').strip()
preferences = {"safebrowsing.enabled": True}
chrome_options.add_experimental_option("prefs", preferences)
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=master_path + "/chromedriver.exe")
driver.find_element_by_xpath("/html[1]/body/[1]/div[37]/div[1]/ul[1]/li[13]").click()

Problem clicking an href=javascript using selenium web drive python

I am having an problem with clicking an menu item that has an link with an href with selenium web drive.
<a class="a-link-normal" href="javascript:;">Fiction</a>
Here is my code:
driver.find_element_by_xpath('//*[#id="div-fiction"]/span/a').click()
This only seem to hover over the linked name and highlight it but does not trigger the javascript. I tried reading all the other solutions but it all lead to just using the click() method. Does anyone know what is wrong or a different approach to this.
The desired element seems to be a JavaScript enabled element so you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:
LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Fiction"))).click()
XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='a-link-normal' and contains(.,'Fiction')]"))).click()
You may try this:
driver.execute_script("arguments[0].click();", webelement to click)

Can someone please explain me how to write an xpath for selenium on python?

I'm having trouble clicking on elements in a web page. I'm trying to do this with:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "..........']"))).click()
The question is how can I write xpaths correctly in all situations so that they suit this function?
Here you can find a good tutorial, which will teach you xPath basics. On the webpage when you press F12 the dev tools opens:
Then you click on one of the elemnts and then Ctrl+f. Then the search menu will open:
There you can type xPaths and CSS Selectors to make sure, that you have found the right element. For example:
once you have typed yor xPath , you will see on the right side the number of the elements found, also you can click on up and down buttons to iterate over every element, to see whic exactly element is on number 1,2...
Once you have found the right xPath, just copy it from dev tools and paste in the code like this:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div"))).click()
In this case it will be selected only one element. If you want to wait until all elements, if there are more than 1, just use one of the EC methods, for example:
WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div")))
this will wait for visibility of all elements located by //div xPath.
Hope this helps.

Categories

Resources