The code I have opens a website and using actionChains, it right clicks on the desktop which brings up a menu. I now need to do 3 more things with actionChains. I need to hover over the item that says Save Page WE and then click an item on the sub-menu that pops up and then hit the enter button. Can anyone show me how to do this? Thanks
from selenium import webdriver
from selenium.webdriver import ActionChains
fp = webdriver.FirefoxProfile('/Users/Jake/AppData/Roaming/Mozilla/Firefox/Profiles/emjx467y.default-1524932841911')
driver = webdriv[enter link description here][1]er.Firefox(fp)
driver.get('http://www.tradingview.com/screener')
element = driver.find_element_by_link_text('Screener')
actionChains = ActionChains(driver)
actionChains.context_click(element).perform()
By using this line : actionChains.context_click(element).perform() , you are trying to right click on Screener menu. But the ideal behavior should be to be hover on it and select one options out of 3.
I'm selecting Forex Screener, you can select any one as per your requirement.
For hover over you can try this code :
actionChains.move_to_element(element).perform()
Full code would be like this :
driver.get("http://www.tradingview.com/screener")
wait = WebDriverWait(driver,40)
driver.find_element_by_css_selector("span[class*='tv-header__dropdown-wrap--noarrow'] span[class$='lang-icon--current-lang']").click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='English (UK)']/parent::a"))).click()
element = driver.find_element_by_link_text('Screener')
actionChains = ActionChains(driver)
actionChains.move_to_element(element).perform()
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Forex Screener"))).click()
Make sure to import these :
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
from selenium.webdriver.common.action_chains import ActionChains
Related
I am using Selenium with Python on a webpage with JavaScript. The script runs until a pop=up asks for a click either to agree or seek More Options labels appear. The HTML for the pop-up disappears if an option is manually clicked. Would appreciate some guidance on how to click "Agree" automatically.
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(executable_path='C:/A38/chromedriver_win32/chromedriver.exe')
driver.implicitly_wait(30)
driver.maximize_window()
# Navigate to the application home page
driver.get("https://www.sportinglife.com/racing/results/2020-11-23")
To click on the button with text AGREE. First induce waits for page load and then wait for the element to be clickable.
driver.get('https://www.sportinglife.com/racing/results/2020-11-23')
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='AGREE']"))).click()
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
This will do the trick:
driver.find_element_by_xpath('//*[#id="qc-cmp2-ui"]/div[2]/div/button[2]').click()
Trying to scrape this webpage for prices, and I need the prices to be in US dollars, so it is currency I understand. However, when I initially load the URL, it gives the prices in multiple seemingly random currencies. I found that I could change this by clicking the next button, and then the back button, but when I tried to automate this, it did not work. Instead, running this code clicks the next button twice, rather than clicking it once, waiting for five seconds, and then clicking the back button. Here is the code that I am currently using that can replicate this problem.
from selenium import webdriver
driver = webdriver.Chrome(r'C:\Users\Hank\Desktop\chromedriver_win32\chromedriver.exe')
driver.get('https://steamcommunity.com/market/listings/440/Unusual%20Old%20Guadalajara')
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import StaleElementReferenceException
import time
time.sleep(5)
action = ActionChains(driver)
next_button=wait(driver, 10).until(EC.element_to_be_clickable((By.ID,'searchResults_btn_next')))
action.move_to_element(next_button).click().perform()
time.sleep(5)
back_button=wait(driver, 10).until(EC.element_to_be_clickable((By.ID,'searchResults_btn_prev')))
action.move_to_element(back_button).click().perform()
Thanks, your time and help is greatly appreciated. Please direct me to a relevant question if this one has already been answered somewhere else.
You don't need ActionChains class, it's works by .click() method.
Try following code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome(r'C:\Users\Hank\Desktop\chromedriver_win32\chromedriver.exe')
driver.get('https://steamcommunity.com/market/listings/440/Unusual%20Old%20Guadalajara')
wait = WebDriverWait(driver, 20)
next_button = wait.until(EC.element_to_be_clickable((By.ID,'searchResults_btn_next')))
next_button.click()
time.sleep(5)
back_button = wait.until(EC.element_to_be_clickable((By.ID,'searchResults_btn_prev')))
back_button.click()
But note, time.sleep(5) is bad way, you can use other way, ex : wait until the second page element appear.
Or instead of time.sleep(...) in this case, you can use this code:
wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR,'.pagebtn.disabled')))
The above is the disable previous button since you landing in the first pagination, and will gone when you arrieve on second pagination. Use .invisibility_of_element_located, it will more efficient.
I'm trying to select 'Newest' from the drop-down menu.
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
# options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=options)
url = 'https://play.google.com/store/apps/details?id=com.whatsapp&hl=en&showAllReviews=true'
driver.get(url)
state_selection = driver.find_element_by_xpath("//div[.='%s']" % "Most relevant")
state_selection.click()
state_selection.send_keys(Keys.UP)
state_selection.send_keys(Keys.UP)
state_selection2 = driver.find_element_by_xpath("//div[.='%s']" % "Newest")
state_selection2.send_keys(Keys.RETURN)
but as soon as it reaches Newest and as I send command to press enter(as shown in code),it resets to "Most Relevent". I'm not able to get my head around on how to achieve this.
After you have clicked state_selection, something like this will click "Newest":
driver.find_element_by_xpath("//div[#role='option']/span[contains(text(),'Newest')]").click()
The more robust method would be working with WebdriverWait to allow the DOM to update, so:
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.XPATH, "//div[#role='option']/span[contains(text(),'Newest')]"))).click()
Note you need these imports for WebdriverWait:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
There are different ways to find
Index
Value
Visible Text
When you use the xpath if the values are changed in future,it pick that element present in that location only.So its better to user select by visible text
state_selection=Select(driver.find_element_by_xpath("//div[.='%s']" % "Most relevant").click();
state_selection.select_by_visible_text("Dropdown Visible Text")
As part of testing using Python and Selenium, I intend to open a youtube link using selenium and want to click on the "AirPlay"button in order to send it across to the Apple TV.
Initially I had the problem with the element being hidden but that was taken care of using the ActionChains. The script executes but I do not see click being executed on the video played by which I could see the AppleTv name show up.
Below is the code:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import os , sys
server_url = "http://10.0.10.4:4444/wd/hub"
capabilities = DesiredCapabilities.SAFARI
driver = webdriver.Remote(desired_capabilities=capabilities,
command_executor=server_url)
driver.get("https://www.youtube.com/watch?v=_YhrCp9m14k")
#air_play = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[#id="move_player"]/div[28]/div[2]/div[2]/button[6]')))
air_play = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, 'ytp-airplay-button')))
#air_play = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[#id="text"]')))
#air_play = driver.find_element_by_css_selector('button.ytp-ariplay-button')
hover = ActionChains(driver).move_to_element(air_play)
hover.perform()
hover1 = ActionChains(driver).click_and_hold(air_play).perform()
The html element is as follows:
<button class="ytp-airplay-button ytp-button" title="AirPlay" style=""><svg
height="100%" version="1.1" viewBox="0 0 36 36" width="100%"><use
class="ytp-svg-shadow" NS1:href="#ytp-id-27"></use><path class="ytp-svg-
fill" d="M12,28 L24,28 L18,22 L12,28 Z M27,9 L9,9 C7.9,9 7,9.9 7,11 L7,23
C7,24.1 7.9,25 9,25 L13,25 L13,23 L9,23 L9,11 L27,11 L27,23 L23,23 L23,25
L27,25 C28.1,25 29,24.1 29,23 L29,11 C29,9.9 28.1,9 27,9 L27,9 Z" id="ytp-
id-27"></path></svg></ button>
The XPATH is as follows:
//*[#id="movie_player"]/div[28]/div[2]/div[2]/button[6]
The CSS Selector is as follows:
#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-
right- controls > button.ytp-airplay-button.ytp-button
Can someone help with as to why the button does not get clicked and show up the available Airplay options?
Before hovering over the video.
After hovering over the video and clicking on the AirPlay button.
I don't have Safari on a Mac so I can't repro this myself but I'm guessing the issue (from your screenshot) is that the icon isn't visible until the video area is hovered. By design, Selenium will not interact with elements that a user can't see. There are a couple ways to do this.
The user way... hover the video, hover the icon, and then click the icon. If you are trying to simulate a user, this is the method you want to use.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
video = driver.find_element_by_css_selector("video")
ActionChains(driver).move_to_element(video).perform()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='AirPlay']")).click()
The non-user way... click on it with JSE. With JSE we can click any element, visible or not. No user can click an invisible element so if you are trying to simulate a user, don't use this... use #1.
airplayButton = driver.find_element_by_css_selector("button[title='AirPlay']")
driver.execute_script("arguments[0].click();", airplayButton);
I'm trying to use Selenium to click the tab for quarterly financials on this page:
http://www.msn.com/en-us/money/stockdetails/financials/fi-126.1.AAPL.NAS
When I run my code, it works some of the time, and sometime it tells me:
"Element is not clickable at point (897.7999877929688, 20.100006103515625). Other element would receive the click:
<span class="mectrlname mectrlsignin"></span>"
Here is the code I am running...
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import *
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
driver.get('http://www.msn.com/en-us/money/stockdetails/financials/fi-126.1.AAPL.NAS')
wait = WebDriverWait(driver, 3)
qtrtab = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[#id="financials-period-list"]/li[2]')))
qtrtab.click()
Does anyone know why sometimes I get the error message and other times it works just fine? Should I be doing this differently? Thanks!
There is a "frozen" header that covers the element you want to click when the cursor is moved to it. Just maximize the browser window to avoid this problem:
driver = webdriver.Firefox()
driver.maximize_window()