Refreshing the source code without refreshing the web page - python

I want to measure the number of cookies regarding of the policie accepted by the user.
So, for example, one the website https://sephora.fr i'm first accessing to the control panel :
driver = webdriver.Chrome(PATH)
driver.implicitly_wait(10)
driver.get(website)
driver.find_element_by_id('footer_tc_privacy_button').click()
Then i would like to click on the black button ("Enregistrer")
driver.find_element_by_id('save-consent').click()
The problem is that the HTML code source is updated after the first click and selenium keep the initial source code -> selenium can't find the button 'save-content'.
Unfortunately i can't refresh the page because it will close the control panel.
I tried to sleep some seconds after the first click, but it's not working.
Any idea ?
Edit : i also tried to switch to the Iframe
frame = driver.find_element_by_xpath('//frame[#name="privacy-iframe"]')
driver.switch_to.frame(frame)
because the button Enregistrer is on this iframe
<iframe id="privacy-iframe" class="tc-reset-css tc-privacy-center-iframe" src="https://cdn.trustcommander.net/privacy-center/default/modern/index.html" title="Vos paramètres cookies" lang="fr"></iframe>
but it's not also not working
EDIT : Solution
I switched to the iframe
frame = driver.find_element_by_id('privacy-iframe')
driver.switch_to.frame(frame)
driver.find_element_by_id('save-consent').click()
then i switched back to the parent
driver._switch_to.parent_frame()

I do not see the name in the iframe tag attribute shared by OP :
try with ID instead :
frame = driver.find_element_by_id('privacy-iframe')
driver.switch_to.frame(frame)

Related

Python Selenium: Click Instagram next post button

I'm creating an Instagram bot but cannot figure out how to navigate to the next post.
Here is what I tried
#Attempt 1
next_button = driver.find_element_by_class_name('wpO6b ')
next_button.click()
#Attempt 2
_next = driver.find_element_by_class_name('coreSpriteRightPaginationArrow').click()
Neither of two worked and I get a NoSuchElementException or ElementClickInterceptedException . What corrections do I need to make here?
This is the button I'm trying to click(to get to the next post)
I have checked your class name coreSpriteRightPaginationArrow and i couldn't find any element with that exact class name. But I saw the class name partially. So it might help if you try with XPath contains as shown below.
//div[contains(#class,'coreSpriteRight')]
another xpath using class wpO6b. there are 10 elements with same class name so filtered using #aria-label='Next'
//button[#class='wpO6b ']//*[#aria-label='Next']
Try these and let me know if it works.
I have tried below code and it's clicking next button for 10 times
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
if __name__ == '__main__':
driver = webdriver.Chrome('/Users/yosuvaarulanthu/node_modules/chromedriver/lib/chromedriver/chromedriver') # Optional argument, if not specified will search path.
driver.maximize_window()
driver.implicitly_wait(15)
driver.get("https://www.instagram.com/instagram/");
time.sleep(2)
driver.find_element(By.XPATH,"//button[text()='Accept All']").click();
time.sleep(2)
#driver.find_element(By.XPATH,"//button[text()='Log in']").click();
driver.find_element(By.NAME,"username").send_keys('username')
driver.find_element(By.NAME,"password").send_keys('password')
driver.find_element(By.XPATH,"//div[text()='Log In']").click();
driver.find_element(By.XPATH,"//button[text()='Not now']").click();
driver.find_element(By.XPATH,"//button[text()='Not Now']").click();
#it open Instagram page and clicks 1st post and then it will click next post button for the specified range
driver.get("https://www.instagram.com/instagram/");
driver.find_element(By.XPATH,"//div[#class='v1Nh3 kIKUG _bz0w']").click();
for page in range(1,10):
driver.find_element(By.XPATH,"//button[#class='wpO6b ']//*[#aria-label='Next']" ).click();
time.sleep(2)
driver.quit()
As you can see, the next post right arrow button element locator is changing between the first post to other posts next page button.
In case of the first post you should use this locator:
//div[contains(#class,'coreSpriteRight')]
While for all the other posts you should use this locator
//a[contains(#class,'coreSpriteRight')]
The second element //a[contains(#class,'coreSpriteRight')] will also present on the first post page as well, however this element is not clickable there, it is enabled and can be clicked on non-first pages only.
As you can see on the picture below, the wp06b button is inside a lot of divs, in that case you might need to give Selenium that same path of divs to be able to access the button or give it a XPath.
It's not the most optimized but should work fine.
driver.find_element(By.XPATH("(.//*[normalize-space(text()) and normalize-space(.)='© 2022 Instagram from Meta'])[1]/following::*[name()='svg'][2]")).click()
Note that the XPath leads to a svg, so basically we are clicking on the svg element itself, not in the button.

How do I fill out an html form using selenium?

url = http://ptvtelecom.com/
If you follow the url and click the button which says 'combrobar' which should be visible in the middle of the screen, it takes you to a form that needs to be filled out. I was wondering how to fill out the form using selenium.
So ive already tried finding the element by id and by name but it does not work. Any help on how to find the element of the first text box for instance would be greatly aprreaciated.
option = webdriver.ChromeOptions()
option.add_argument(" — incognito")
browser =
webdriver.Chrome(executable_path='/Users/grsanchez/downloads/chromedriverM',
options=option)
browser.get('http://ptvtelecom.com/')
browser.find_element_by_xpath('//*
[#id="cobertura"]/div/div[2]/div/div/p/a').click()
Here is where it goes wrong
name = browser.find_element_by_id('nombre')
name.send_keys('user1')
read the comments in the code to understand why your code isn't working.
basically, you're trying to select something that exists inside an iframe.
option = webdriver.ChromeOptions()
option.add_argument("--incognito")
browser = webdriver.Chrome(executable_path='/Users/grsanchez/downloads/chromedriverM',
options=option)
browser.get('http://ptvtelecom.com/')
## finding the button that shows the form
btn = browser.find_element_by_css_selector('#cobertura .boton-cobertura')
## using js to click it, to avoid getting issues in case the button wasn't visible
driver.execute_script("arguments[0].click();", btn)
## the element you want to select is actually inside an iframe, so we need to switch to it, if we want to select anything
driver.switch_to.frame(driver.find_element_by_css_selector('#popmake-1432 iframe'));
## selecting the name input and sending a string
name = driver.find_element_by_css_selector('#nombre')
name.send_keys('user1')
PS to return to the main frame, you can do this :
driver.switch_to.default_content()
you need to switch to iframe something like driver.switchTo().frame("a077aa5e");
then use your locators inside the iframe

How to find and click on HTML image element

I'm new with Selenium and struggle with this one for few hours.
I have an HTML page that contains icon and stream view (both images), the browser view is on 100% and I would like to modify it and make it smaller,
it can be done by changing browser zoom, modifying CSS property or clicking on the image.
My code starts with open browser and waits till all elements load:
driver = webdriver.Firefox()
driver.get('http://' + user +':'+password+'#'+camera_address)
driver.maximize_window()
driver.implicitly_wait(15) # seconds
I tried to do so via zoom:
driver.execute_script("document.body.style.zoom='0.4'")
Didn't worked.
Tried to do it via Selenium -- find element and change CSS:
myDynamicElement = driver.find_elements_by_id("stream")
or find the image and click it with:
driver.find_element_by_xpath('//img[#src="../pics/button_downsize_27x27px.gif"]').click()
or
driver.find_elements_by_xpath('//*[#img]').click()
or
driver.find_elements_by_tag_name('img').click()
The HTML page looks like this:
<img src="/pics/button_downsize_27x27px.gif" width="27" height="27" border="0" title="Scale down to 800 px width" alt="Scale down to 800 px width">
<td colspan="3" align="center"><img id="stream" src="/mjpg/video.mjpg" width="2560" height="1920" border="0" alt="If no image is displayed, there might be too many viewers, or the browser configuration may have to be changed. See help for detailed instructions on how to do this."><br></td>
Tried with #title and #alt and even contain but nothing works.
What am I doing wrong?!?! How to find and click on this image (/pics/button_downsize_27x27px.gif)?
To click on your desired image -- please use the reference code mentioned here.
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy((By.cssSelector("a"))));
List<WebElement> elem = driver.findElements(By.tagName("a"));
for(WebElement el : elem){
String element = el.getAttribute("src");
if(element.contains("/pics/button_downsize_27x27px.gif")){
el.click();
break;
}
}
It is in Java, you can implement it in Python.
Thank you all, finally, I did a little workaround since I saw that in chrome there is a little bug and via Firefox, I cannot find the element.
So I retrieve again the URL after all elements loaded and modify it so the JavaScript function will be triggered:
myElem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, 'indexMain')))
driver.get(driver.current_url+'&size=8')
Thank you all for trying, this one was a challenge since the element have no id/name/class/css and the browser could not find the XPath.

Selenium Hover/Click event on ajax filled menu options

Having serious issues here. Someone please help.
I am trying to login to a website. - This Works!
Redirect to the page I want after Login - This Works!
Now once in, I have to hover over the settings icon so the dropdown shows, and then click on the "Settings" options that has NO ID or CLASS or HREF.
Now there is a couple of reasons I cant do this. Number 1 is if I try to click on the link after the hover, it tells me that it's hidden and I cant interact with it. Also the menu options in the form are populated and appended once you hover, through ajax I think. They are not on the initial page load.
wait = WebDriverWait(driver, 10)
box = wait.until(EC.visibility_of_element_located((By.ID, "yucs-help_button")))
menuButton = driver.find_element_by_id("yucs-help_button")
ActionChains(driver).move_to_element(menuButton).perform()
After the above code is deployed I print the driver.page_source and can see (below) that the new menu options are there, if you don't hover, the below code will not be on the page.
Now the <a> i'm trying to click is the <span>Settings</span> option, and for the life of me, it will not work. Either can't find it, not clickable, can't interact with it, etc, etc, etc. xpath, css_selector, partial_name, nothing finds this thing. Also whats weird is once you click on it, from a browser, it appends an ID to <span> So weird. Any ideas?
<a data-ylk="rspns:nav;t3:tl-lst;t5:custitm;slk:custitm;elm:itm;elmt:custitm;itc:0;cpos:2" class="C(#000)! Td(u):h " data-mad="options" data-customevt="true" href="#" data-rapid_p="18"><span>Settings</span></a>
To perform mouse over event on element you should try to use .execute_script() using following java script :-
wait = WebDriverWait(driver, 10)
box = wait.until(EC.visibility_of_element_located((By.ID, "yucs-help_button")))
menuButton = driver.find_element_by_id("yucs-help_button")
driver.execute_script("var clickEvent = document.createEvent('MouseEvents');clickEvent.initEvent('mouseover', true, true); arguments[0].dispatchEvent(clickEvent);", menuButton)
Now after successfully mouse over you should try to click on Settings link as below :-
driver.find_element_by_xpath("//span[contains(text(), 'Settings')]/parent::a[#data-mad = 'options']").click()
Hope it will help you..:)

How to access elements of page after login selenium?

I can successfully login using the selenium web driver, but I don't know how to access the frame on the next page. I have tried setting the new frame, but it does not find that element because I think it is looking through the elements on the login page instead of the page after. This is the url after the login is successful. https://homeaccess.katyisd.org/HomeAccess/Classes/Classwork
Path of frame on page AFTER login page
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = "https://homeaccess.katyisd.org/HomeAccess/Account/LogOn?ReturnUrl=%2fHomeAccess"
browser.get(url)
browser.find_element_by_id('LogOnDetails_UserName')
browser.find_element_by_id('LogOnDetails_Password')
browser.find_element_by_id('LogOnDetails_UserName').clear()
browser.find_element_by_id('LogOnDetails_Password').clear()
browser.find_element_by_id('LogOnDetails_UserName').send_keys('******')
browser.find_element_by_id('LogOnDetails_Password').send_keys('******')
This is all on the page after the login
frame = browser.find_element_by_xpath('//*[#id="sg-legacy-iframe"]') //prints no such element found
browser.switch_to_frame(frame)
browser.find_element_by_xpath('//*[#id="SignInSectionContainer"]/div[2]/button').click()
Try to wait for frame to appear:
frame = WebDriverWait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'sg-legacy-iframe')))
After performing click on Login please wait for some time to load the page.
You can use WebDriverWait for waiting for required element
if above one fails generally in java in will go for Thread.sleep(5000);
As per provided HTML code you can try switching to frames by other locators if id fails
driver.switchTo().frame("sg-legacy-iframe"); //by providing id
driver.switchTo().frame(driver.findElement(By.xpath("//div[#id='MainContent/iframe"))); //by providing xpath
Thank You,
Murali

Categories