Selenium Hover/Click event on ajax filled menu options - python

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..:)

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.

Refreshing the source code without refreshing the web page

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)

Having a hard time clicking on a radio button that activates a drop down. Using Selenium and Python

SO normally I don't have a hard time with radio buttons but I was trying to configure the web browsers set home page to custom URL from the chrome settings. I knew I would have to activate the drop down box that lets me (picture provided). I located what I think is the source and had to navigate my way through some shadow DOM's. however after getting to the path I tried to click on it I get the error
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (311, 1418)
I'm confused and I've been fighting this off for a while. Any one have any idea? I did notice some of the settings change when I personally click on the different options. here are the pictures
Heres my code:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
def expand_shadow_element(element):
shadow_root = cdriver.execute_script('return arguments[0].shadowRoot', element)
return shadow_root
#chrom driver
cdriver = webdriver.Chrome(executable_path='C:\\Users\\name\Downloads\\chromedriver.exe')
#open up page to chrome settings.
cdriver.get('chrome://settings/')
root1 = cdriver.find_element_by_tag_name('settings-ui')
shadow_root1 = expand_shadow_element(root1)
root2 = shadow_root1.find_element_by_id('main')
shadow_root2 = expand_shadow_element(root2)
root3 = shadow_root2.find_element_by_tag_name('settings-basic-page')
shadow_root3 = expand_shadow_element(root3)
root4 = shadow_root3.find_element_by_tag_name('settings-on-startup-page')
shadow_root4 = expand_shadow_element(root4)
root5 = shadow_root4.find_element_by_name('4')
shadow_root5 = expand_shadow_element(root5)
root6 = shadow_root5.find_element_by_id('button')
root6.click()
anyone have any idea why I cant click on the source? i even right clicked on the radio button and thats the source I was pointed to.
To work around the element click intercepted error, you can try a Javascript click and see if that works for you.
The DOM tree is a bit hard to follow, but I think I catch the drift here -- a radio setting is under controlled-radio-button element, and the little circle itself is <div class='disc'>, as highlighted in your code sample.
I didn't see a shadow root under <div id="button"> that you store in root6 -- I see the element, but not a shadow root, so I am assuming the radio button itself actually lives under root5.
With that being said, here's some code to click on a radio button (given its description) using Javascript:
# grab the radio element... tricky with shadow roots
radio_button = root5.find_element_by_xpath("//div[#id='button']/div[#class='disc']")
# attempt to click it using JS -- ignores ClickIntercepted error
driver.execute_script("arguments[0].click();", radio_button)
Because you have already located the correct radio group and stored the shadow elements in root5 variable, you can try to use find_element_by_xpath on root5 to grab the <div class="disc"> element that appears right under it. The Javascript click should hopefully work around the ClickIntercepted error.

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

Selenium driver doesn't click on element sometimes

currently i'm writing a web scraper that will work in infinite loop. It gets a page, searches for some buttons and clicks one of them. But sometimes it doesn't! I save a screenshot in case of some fail and it showed me that page didnt changed after button clicked.
driver.find_element_by_xpath('//input[#name = "btn"]').click()
time.sleep(3)
I have bypassed this with a loop checking does we see that element still.
while driver.find_elements_by_xpath('//input[#name = "Submit"]') != []:
driver.find_element_by_xpath('//input[#name = "Submit"]').click()
But hope to find a root cause of this. What it could be?
I also faced a similar problem with my application. Clicking the element through action class worked for me.
Here is the sample code in Java:
WebElement webElement = driver.findElement(By.id("Your ID Here"));
Actions builder = new Actions(driver);
builder.moveToElement(webElement).click(webElement);
builder.perform();
If clicking with action class does not work, you can also try clicking element by Javascript.
WebElement webElement = driver.findElement(By.id("Your ID here"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", webElement);

Categories