Selenium action chains to click inside silverlight plugin object in a page - python

Currently trying to click a button inside of a silverlight web app, and yes I know beforehand silverlight is not supported but I think at least it worth a try with the action chain module. Here is the best I could come up with until now:
profile = FirefoxProfile()
profile.set_preference('plugin.default.state', 2)
driver = webdriver.Firefox(profile)
driver.get('http://www.trypython.org')
time.sleep(8)
sldiv = driver.find_element_by_xpath('//*[#id="silverlightControlHost"]')
builder = ActionChains(driver)
builder.move_to_element_with_offset(sldiv, 372, 44)
builder.click()
IT is not clicking, or at least not in the expected coordinates (where the button is). Will I get away with this? if so, will I be able to type some text after clicking inside a textbox?
Do not even mention silverlight-selenium outdated project

Related

Python Selenium, edge browser, I dont see Inspect Element on every element

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()

Selecting span-like button with Selenium and Python

I am very new to Selenium and I have been trying to create a scraper to get some information from Brazil's treasury website to then perform some simulations. In order to reveal part of the input boxes on the website (https://www.tesourodireto.com.br/titulos/calculadora.htm) it is necessary to select a button that was created as a span (image here - the arrow is on the right-hand side of the page).
I managed to select the element, but since it is truly a span (and not a button), I couldn't use the .click() method, and since the input boxes are hidden, I can't fill them. Does anybody have an idea how to work around this?
driver = webdriver.Chrome(PATH)
driver.get("https://www.tesourodireto.com.br/titulos/calculadora.htm")
driver.find_element(By.XPATH, '/html/body/main/div[5]/div/div[1]/div/form/div[2]/h2/span').click()
Exception has occurred: ElementNotInteractableException
Message: element not interactable
I used SeleniumBase to perform js_click actions where regular clicks failed. Here's the full script for that:
from seleniumbase import BaseCase
class MyTestClass(BaseCase):
def test_base(self):
self.open("https://www.tesourodireto.com.br/titulos/calculadora.htm")
self.click("div#onetrust-close-btn-container button")
self.js_click("span.perfil-notice__close")
self.js_click("h2.td-calc-form__avancedsimu span")
self.highlight("input#dataVenda")
self.type("input#dataVenda", "31/12/2022")
self.highlight("input#taxaPapelVenda")
You'll need seleniumbase.
Tests are run with pytest.

Using Python & Selenium Package to create a script that switches the Chrome Dark Mode on off. How to select element in chrome://?

driver = webdriver.Chrome(chromedriver)
driver.get("chrome://flags/")
wait = WebDriverWait(driver, 10)
try:
firsts = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "#enable-force-dark")))
firsts.click()
seconds = wait.until(EC.presence_of_element_located(By.NAME, "enable-force-dark_name"))
seconds.click()
I'm trying to use selenium to go to chrome://flags/ and enable / disable the force dark mode theme. I have tried to use x-path, class etc to reach this button but for some reason i cant access it. would it be better for me to try to use a different method of selecting this drop down list? thanks
this is not proper handling of browser settings in automation. you should control your browser via capabilities.
capabilities.chromeOptions.args = ['--force-dark-mode']
// Forces dark mode in UI for platforms that support it.

How to click on an element based on its coordinates with selenium webdriver

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.

WebDriver does not return to default content

I'm trying to write a test for a pair of sites taking part in the online support system: one is for Agent, the second is for the Visitor.
Initially, the Visitor opens the support tab on his site and the Agent gets the call on his side and picks it up. Both sides get the text chat only at this moment.
After that, the Agent wants to escalate the chat-call to Audio-Video level and presses a special button for that.
An additional widget opens containing the IFRAME element with INVITE button.
Agent presses the INVITE button.
After that the Agent has to press the Send button in the textual chat (that is on the main page content, not in the IFRAME) to send the invitation for the agent to join the Audio-Video chat.
These are the steps that I want to automate.
Everything is working until the .switch_to_default_content(). After that, the browser seems to lose the DOM and does not find neither elements on the main page nor inside the IFRAME.
Behavior is the same for local WebDriver and for WebDriver.Remote grid, for Chrome latest and Firefox latest, on Windows 10 and on Mac OS X 10.11.6 regardless of Capabilities combination.
Below is the code in Pytnon:
#Starting the Agent browser
options = webdriver.ChromeOptions()
options.add_argument("--disable-notifications")
agent_browser = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=options.to_capabilities())
<...>
#Starting the Visitor browser
visitor_browser = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=DesiredCapabilities.CHROME)
<...>
# Finding the IFRAME
agent_LPVE_iframe = WebDriverWait(agent_browser, 30).until(EC.presence_of_element_located((By.XPATH, "//iframe[contains(#class, 'lpview_table_items_placeholder')]")))
# Switching to IFRAME
agent_browser.switch_to_frame(agent_LPVE_iframe)
<...>
# Switching to default contents
#agent_browser.switch_to_default_content()
agent_browser.switch_to.default_content()
After this, the WebDriver becomes unable to find any element in both main page DOM and inside the IFRAME even if it gets switched back to IFRAME.
Why it happens and how do I make it work correctly?
I am using:
- Python 3.6.0 x32
- selenium-server-standalone-3.3.1.jar
- ChromeDriver 2.28 Win32
- GeckoDriver 0.14.0 x64
Tell me if you need any additional information.

Categories