I try to click on plus by xpath. But this plus becomes visible after the mouse cursor on it and my code can't click on this.
driver.find_element_by_xpath('/html/body/div[3]/div/div[5]/div[3]/div[12]/div[2]/div[2]/div[1]/div/div/div/div/div[2]/div/div/div[3]').click()
Is there any way in selenium webdriver to click on invisible elements
Hi please do it like below (code sample)
https://selenium.googlecode.com/git/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html
menu = driver.find_element_by_css_selector("")
hidden_submenu = driver.find_element_by_css_selector("")
ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
You can click via javascript:
driver.execute_script("arguments[0].click();", element)
Though, I would use "Action Chains" to perform the mouse over + click.
Related
I am trying to get data from a Power Bi table. There are some elements that appear when hovering over a table. When I right click on ... I don't see Inspect Element. However, when I left click on this element, I can see a menu, and if I right click on any items, I can see Inspect element.
My first question, is why I don't see Inspect Element in the right click menu for all elements in the browser. Am I somehow able to open this ... menu programmatically in Selenium?
the Export Data element only appears in HTML after the first left click. I'm assuming this is created using Javascript and in order to export data with Selenium I would have to programmatically instantiate this by clicking on the ... menu. Is selenium capable of triggering javascript functions that generate more html code in a dynamic webpage? Or do I need to somehow click on the ... element.
If I can execute a javascript function, how can I find out in Edge the javascript function that gets executed and how can I replicate this function in Selenium
Essentially, if I try to find the Export data element in Selenium, it is not able to find it, unless I set a breakpoint before search, then in EdgeDriver I open this menu, and then I can find it and click it through Python
If all else fails, can I programmatically open the left click menu by automating a mouse click at certain coordinates in Selenium?
1.1 why I don't see Inspect Element in the right click menu for all elements:
PowerBi has its own context menu so they suppress the browsers context menu. If the element is tricky to find the dev tools, you can press Ctrl + Shift + C (while dev tools is open) and then click the desired element. Your mouse needs to be already over the element before pressing the key combination.
1.2 Am I somehow able to open this ... menu programmatically in Selenium?
Seems a little tricky, but could work if you first find the title of that area and move the mouse there, like described here: https://stackoverflow.com/a/8261754/12914172
Then your element should be in the html and you can find it hopefully by its class name vcMenuBtn that seems to be unique on that page. But you need to verify that.
2. Is selenium capable of triggering javascript functions that generate more html code in a dynamic webpage? Or do I need to somehow click on the ... element.
Selenium is able to execute javascript like desribed here: https://stackoverflow.com/a/70544802/12914172
However in your sample, and I was quickly checking the PowerBI online page, this looks like a whole lot of reverse engineering to understand and can sometimes be dangerous as well. I would go for hoover over the area find the ... and click it.
3. How can I find out in Edge the javascript function that gets executed
In dev tools you can set breakpoints to debug the steps the pages does after an action. But again, I would not invest to much time in that.
4. Can I programmatically open the left click menu by automating a mouse click at certain coordinates in Selenium?
Yes but this never works as good as the way described above. If you still want to give it a try, maybe that answer helps: https://stackoverflow.com/a/26385456/12914172
Many thanks to r000bin, this solution works for me, downloading data from PowerBI using Selenium for Python:
import selenium, mouse, time
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
url = 'https://dataport.gasunie.nl/Vulling-Gasopslagen-Nederland'
driver = selenium.webdriver.Chrome(service=Service())
driver.get(url)
time.sleep(4)
#driver.fullscreen_window()
#driver.switch_to.window(driver.current_window_handle)
time.sleep(4)
iframe = driver.find_elements(By.TAG_NAME, 'iframe')
assert len(iframe)==1
driver.switch_to.frame(iframe[0])
time.sleep(4)
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.move_to_element_with_offset(driver.find_element(By.TAG_NAME, 'html'), 0,0)
actions.move_by_offset('5', '5').click().perform()
time.sleep(4)
button = driver.find_element(By.CLASS_NAME, 'vcMenuBtn')
button.click()
button = driver.find_element(By.ID, '0')
button.click()
# 4 tabs and 1 enter
time.sleep(4)
for n in range(4):
element = driver.switch_to.active_element
time.sleep(2)
element.send_keys(Keys.TAB)
time.sleep(2)
element = driver.switch_to.active_element
time.sleep(2)
element.send_keys(Keys.ENTER)
driver.close()
I am using the selenium webdriver in Python to process through a website, but I am running into an issue with clicking a specific button that shows up in a popup modal. Currently, the behavior seems as though the click is being performed, but appears to be clicking the "No" button instead of "Yes."
Here is the snippet of code I am currently using. The first click will hit an "Apply" button, which then opens a popup confirmation modal that requires the user to select No or Yes to continue with applying the request.
driver.find_element(By.CSS_SELECTOR, xpaths['planRangeApply']).click()
time.sleep(2)
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.modal-footer:nth-child(3) > jv-button:nth-child(2)'))).click()
For comparison, here is the CSS_SELECTOR for the "No" button:
div.modal-footer:nth-child(3) > jv-button:nth-child(1)
From what I have researched so far, I am properly waiting for the modal to appear and the proceed to find the Yes button element. Am I misunderstanding this process or is my CSS_SELECTOR path simply incorrect? I have tried utilizing XPATH instead of CSS_SELECTOR, but I get the exact same result.
Here is a screenshot of the page inspector with the correct button highlighted:
The CSS_SELECTOR is slightly off.
The parent tag is <div class="modal-footer">
The desired element is the second <jv-button> child of it's parent.
Solution
The effective CSS_SELECTOR would be:
div.modal-footer jv-button:nth-child(2)
I often encounter elements which I cannot right click to inspect their xpath or css.
I want to ask what other ways exist to click on those elements ?
You can use Ctrl + Shift + C, it will open the devtools with selecting an element to inspect enabled. Just move the mouse cursor to the element and click, it will scroll the html view in devtools to the correct place.
Alternatively, you can press F12 and toggle the selecting an element to inspect button (top left corner of the devtools).
There are a lot of elements/controls which can't be located within the HTML DOM i.e. you can't right click on the element to inspect their xpath through google-chrome-devtools. The three mostly encountered such elements/controls are as follows:
Alert: Alerts are created through JavaScript and you can't find them within the DOM Tree. You can find a detailed discussion in Why switching to alert through selenium is not stable?
Notifications (PasswordRemember/GeoLocation/Microphone/Camera):
These notifications can't be tracked within the DOM Tree. Here you will find a relevant discussion on How to allow or deny notification geo-location microphone camera pop up
Are you sure you want to leave this page? popup:
This message as well can't be located within the DOM Tree. Here you will find a detailed discussion on How to handle below Internet Explorer popup “Are you sure you want to leave this page?” through Selenium
If you want to get elements locator but the right click doesn't work then try the following.
First, Open the dev tools window by pressing Ctrl+Shift+I.
If that doesn't work then first open the dev tool then load the page.
After opening the dev tools click on "select element" tool, the printer icon at the left of the dev tool. You can directly get this tool by pressing Ctrl+Shift+C.
Then hover on the element that you want to get the locator. The element will be selected in the DOM in the elements tab. Right click on the elements in the DOM.
Then from the context menu go to copy -> copy selector for CSS selector or copy XPath for XPath.
Now you have got the locator of that element in the clipboard.
Control + Shift + C or F12 will open the dev tools for you, you can then click on the cursor mode on your browser.
So I am trying for over an hour now to make selenium click the youtube like button...
Nothing works on google and I have no idea what to do anymore.
If someone can help me that would be amazing
(Im kinda new to python)
Thanks!
So following on my comment, you need to inspect the xpath of the youtube like button.
Right click on the like button, press inspect element. It should show a console and in the console the like button path should be highlight. Click on it then click copy xpath.
After that do what you need to do, e.g like_button_click = driver.find_element_by_xpath('xpath of the like button').click()
Selenium docs: https://www.seleniumhq.org/docs/
The below is tested and working - remember that to like videos, you always need to be logged into your Youtube account.
1) Identify the buttons xPath:
To extract the specific Like button xPath, you can inspect the button through any developer browser tool, right click on it and select Copy xPath:
Then just copy the xPath into here:
button = driver.find_element_by_xpath("here")
2) Log into Youtube and click on the Like button
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
driver = webdriver.Chrome()
youtube = driver.get("https://www.youtube.com/watch?v=9fHt-VVG_hg")
sleep(5)
button = driver.find_element_by_xpath("//*[#id='top-level-buttons']/ytd-toggle-button-renderer[1]/a")
ActionChains(driver).move_to_element(button).click(button).perform()
Try this one
//yt-animated-icon[contains(#animated-icon-type,"LIKE")]
So, the web app we're developing has a TV/PC mode, and i'm testing the ability to transit between these two modes. `
def pc_to_tv(self):
pc_to_tv = self.driver.find_element_by_xpath(
'html/body/div[1]/div/topbar/header/div[2]/div[2]/div[1]/button[1]')
pc_to_tv.click()
def tv_to_pc(self):
tv_to_pc = self.driver.find_element_by_xpath(
'html/body/div[1]/div/topbar/header/div[2]/div[2]/div[1]/button[2]')
tv_to_pc.click()`
The problem is, when i switch from pc to tv, the screen "zooms in", making the button appear in the same place it would be without the zoom. so, i can't click on the button with my 'tv_to_pc' method, 'cause instead on clicking on the actual button, it clicks where the button should be.
So, the solution i found was clicking on the button with coordinates, that way i'll actually click on the place i want, instead of clicking on an unclickable place like i was doing.
The thing is, i don't know how to do this, and need help on this matter.
I would suggest that you just click the button using JavaScriptExecutor. It will click it no matter where it is on the page. See How to execute a javascript in a Python webdriver and other questions for more info. The general format is
element = driver.find_element_by_id("someId")
driver.execute_script("arguments[0].click();", element)
Also... you don't want to use XPaths like that. Any XPath that starts at the HTML tag or is more than just a few levels deep is going to be very brittle. Do some googling on selenium xpaths and read some guides for more info.
try moveToElement and then perform click
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
new Actions(driver).moveToElement(element, x, y).click().perform();
x is xoffset
y is yoffset
Please see that if you use Javascript for click its not going to be native click.