I'm trying automating a website and I'm trying to access a menu > submenu and perform a click on the item in submenu. After a lot of search I learned that mouseover is a better option for me as the submenu only appear if I hover the mouse on the menu. I have tried many instances but the mouse just won't hover on the main menu and to submenu. The code is:
policymngmt = driver.find_element_by_xpath('/html/body/app-root/div/div/main/site-header/div[1]/div[1]/div[2]/div/div/div/div/div[2]/ul/li[2]/a')
issuance = driver.find_element_by_xpath('/html/body/app-root/div/div/main/site-header/div[3]/div/div[1]/nx-link/a')
actions = ActionChains(driver)
actions.move_to_element(policymngmt).move_to_element(issuance).click().perform()
Try the below
actions = ActionChains(driver)
policymngmt = driver.find_element_by_xpath("xpath")
hover= ActionChains(driver).move_to_element(policymngmt)
hover.perform()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/app-root/div/div/main/site-header/div[3]/div/div[1]/nx-link/a"))).click()
Imports
from selenium.webdriver.common.action_chains import ActionChains
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
Related
I am trying to use selenium to click certain buttons within the bank of america simulator, but the buttons don't seem to ever click. No new link is reached, which is something I haven't encountered before.
https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/
I want to click "Sign in options" and then click "Sign in: Recognized device"
I tried using selenium to click the button and I get no error. Nothing happens at all and the program continues, so I know it's not an issue with not finding the button. My current code is as follows:
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
sleep(3)
login_button = driver.find_element("id", "landing_sign")
driver.execute_script("arguments[0].click();", login_button);
This code worked fine for me.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "landing_sign")).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[aria-labelledby='signInOpt3']")).click()
NOTE: Using sleep is a bad practice, use WebDriverWait and wait for the specific state you need instead.
To click on the clickable elements you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategies:
Code block:
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#landing_sign"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='/onlinebanking_demo/OLB_Simulator/SignIn/recognized']"))).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:
I'm trying to scrape some titles of the videos and to do so I'm using Selenium, but I've encountered a problem. driver.find_element().text returns empty string, but title is for sure located in given XPATH. Here is the fragment of the page source returned by driver.page_source:
<div class="title">Big.Sky.S03E01.ITA.WEBDL.1080p</div>
To find the title I am trying to use:
hoverable = driver.find_element(By.XPATH, '//*[#id="videojs"]/div[1]')
ActionChains(driver).move_to_element(hoverable).perform()
wait = WebDriverWait(driver, 20)
title_from_url = wait.until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[1]/a'))).text
title_from_url = driver.find_element(
By.XPATH, '/html/body/div[1]/a'
).text.casefold()
From what I've read it could be caused by the fact that the page might not be fully loaded (I wasn't using any wait condition here). After that I've tried to add a wait condition and even time.sleep(), but it didn't change anything. <mini question: how would proper wait staitment look like here?>
Edit:
I think the problem is caused, because title is showing up only when mouse is in the player area. I think some mouse movement will be needed here, but I have tried to move mouse into the player area and for some time it is working but after a while there is a moment when title will disappear too fast. Is there a way to use find_element() while also moving mouse?
Any help will be appreciated.
Best regards,
Ed.
Example site: https://mixdrop.to/e/4n3x7e31hpwxm8.
You have to wait for element to be completely loaded before extracting it text content. WebDriverWait expected_conditions explicit waits should be used for that.
This should wait in case the element is visible on the page and the locator is correct:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
title_from_url = wait.until(EC.visibility_of_element_located((By.XPATH, '//div[contains(#class, "title")]/a'))).text
UPD
In case the element content is dynamically changes and we need to hover over that element to make the desired text to appear there, we can simulate the mouse hover action with the help of ActionChains module.
The tool-tip etc. will not disappear until you perform some click etc. on that page. So, just performing a background action of driver.find_element() will not affect that text.
UPD2
The title element is not visible. It becomes visible only by hovering over the player.
Here I'm performing such hovering and then getting the title text:
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
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")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 5)
actions = ActionChains(driver)
url = "https://mixdrop.to/e/4n3x7e31hpwxm8"
driver.get(url)
player = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'player')))
actions.move_to_element(player).perform()
title = wait.until(EC.presence_of_element_located((By.XPATH, '//div[contains(#class, "title")]/a')))
print(title.text)
The output is:
Big.Sky.S03E01.ITA.WEBDL.1080p
If you suspect its because of sync issue. You can use selenium waits.Let it be implicit of explicit.
Implicit:
objdriver.implicitely_wait(float)
Explicit:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
objwait=WebDriverWait(driver,float,poll_frequency=float,ignored_exception=float)
objelement=objwait.until(EC.visibility_of_element_located((By.XPATH,"Your XPATH")))
In the website, I want to scrape there are different dropdown lists. I want to open before copying data.
I have structured the flow like this:
buttons = driver.find_elements(By.CSS_SELECTOR, 'svg[class = "undefined event__expander event__expander--close"]')
for button in buttons:
button.click()
But if I run it, I can open only the first list and the others are still closed.
Any tips?
driver.maximize_window()
wait=WebDriverWait(driver,20)
driver.get('https://www.diretta.it/')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button#onetrust-accept-btn-handler"))).click()
buttons = driver.find_elements(By.CSS_SELECTOR, 'svg[class = "undefined event__expander event__expander--close"]')
for button in buttons:
button.click()
It might have been due to the accept cookies but your code works after you click it.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I'm using Selenium in Python to log into a site and then set up certain values some of which are held in dropdown menus. In Chrome, when I inspect the dropdown menu, I get the following information:
The dropdown menu looks like this with these options:
There are multiple dropdown menus on this page. How would I change this specific dropdown menu's value when it seems that there is no unique ID for this element?
Here's the code I have so far for clicking on the login button, entering my credentials and logging in:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
PATH = "/Users/mikefenty/Documents/chromedriver"
driver = webdriver.Chrome(PATH)
wait = WebDriverWait(driver, 10)
driver.get("https://www.binarycent.com/")
link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Login")))
link.click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[ng-model='email']"))).send_keys(<login ID>)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[ng-model='password']"))).send_keys(<password>)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class*='ui primal']"))).click()
#EC.visibility_of_element_located(By.CSS_SELECTOR, "button[class='ui button green']"))
driver.quit()
I am trying to scrape some google data but I first want to click the 'I agree' button that google pops up. This is the script I use to do that:
import time
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
search_question = input("Ask a question: ")
driver = webdriver.Chrome("*Your Webdriver location*")
driver.wait = WebDriverWait(driver, 5)
driver.get("https://google.com")
time.sleep(1)
agree = driver.wait.until(EC.presence_of_element_located((By.XPATH, '//*[#id="introAgreeButton"]/span/span')))
agree.click()
# time.sleep(0.2)
search = driver.find_element_by_class_name("gLFyf")
search.send_keys(search_question)
search.send_keys(Keys.ENTER)
The problem is selenium doesn't seem to locate the button and therefore I get a timeout error. (I have tried also with find_element_by_xpath and still not working).
If you scroll up in the devtools inspector you'll notice that your element is within an iframe:
You need to switch to that frame first, click your button then switch back to the default content (the main page)
driver.get("https://google.com")
#active the iframe and click the agree button
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe")))
agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="introAgreeButton"]/span/span')))
agree.click()
#back to the main page
driver.switch_to_default_content()
That works for me.
FYI - There's only 1 iframe on the page, that's why the xpath //iframe works. If there were multiple you'd need to identify it with higher accuracy.
If you already have been agreed,then agree button would not appear. That why it is not be able to find given XPath.
Try this:
import time
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
search_question = input("Ask a question: ")
driver = webdriver.Chrome(".\chromedriver.exe")
driver.wait = WebDriverWait(driver, 5)
driver.get("https://google.com")
time.sleep(3)
# agree = driver.wait.until(EC.presence_of_element_located((By.XPATH, '//*[#id="introAgreeButton"]/span/span')))
# agree.click()
# time.sleep(0.2)
search = driver.find_element_by_class_name("gLFyf")
search.send_keys(search_question)
search.send_keys(Keys.ENTER)
Managed to get it working.
Seems like one of the few elements that are interactive in the popup when you first load the site is the language dropdown, which I found by class name.
Then the Agree button is detectable and you can find it by class too.
driver.get("https://www.google.com")
driver.maximize_window()
time.sleep(2)
#finds+clicks language dropdown, interaction unhides the rest of the popup html
driver.find_element_by_class_name('tHlp8d').click()
time.sleep(2)
#finds+clicks the agree button now that it has become visible
driver.find_element_by_id('L2AGLb').click()
you should now have the standard searchbar etc
i had some problems with clicking pop-ups and the problem turned out to be with that click().
im not sure that it might be the same issue with urs or not but try changing ur click to this :
agree = driver.find_element_by_xpath('//*[#id="introAgreeButton"]/span/span')
driver.execute_script("arguments[0].click();", agree)
The problem was that it didn't change frames here is the soulution code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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('C:\\Users\\gassp\\OneDrive\\Namizje\\Python.projects\\chromedriver.exe')
url = 'https://www.google.com/maps/'
driver.get(url)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[#id="consent-bump"]/div/div[1]/iframe')))
agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="introAgreeButton"]/span/span')))
agree.click()
#back to the main page
driver.switch_to_default_content()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="searchboxinput"]'))).send_keys('gostilne')
driver.submit()