python selenium move_to_element_with_offset, passing in float offset - python

The following code, moves to the mid point of the element & performs the click
action_chains = ActionChains(driver)
action_chains.move_to_element_with_offset(element, 33.8333, 0).click().perform()
But the following code, actually clicks at 34px from the top right corner of the element(which is desired).
action_chains = ActionChains(driver)
action_chains.move_to_element_with_offset(element, 34, 0).click().perform()
But I want to actually click at 33.8333 px from the top-right corner, are there any issues if I pass in float values instead of integers ? Any workarounds or am I missing something?

I am not sure about the specific case you are facing, but I have heard of people struggling with similar issues. If I were you, I would look for a javascript solution in conjuntion with driver.execute_async_script

Related

Using selenium in Python, I need to search the page for a given string, then click the dynamic button element next to the string

Imagine I am using a website interface, similar to an online survey. My code can navigate and complete the form by clicking the necessary button. I need to be able to identify which question to click based on a given input.
key_str = '10.'
element = driver.find_element(By.ID, key_str)
We now have to move a few elements forward to click the box that will exist to the right of it. I cannot simply direct xpath to the button, as I will not know where it will be prior to opening the webpage. This needs to be a 1 size fits all type of solution. That said, we will always find "Question #10", and there will always be a button nearby the question.
Ideally the code will be able to identify the position that the "key_str" element resides, feed back this positioning as some sort of variable, then use that variable to find 'button' element that is to the right of it on the page.
Visually: (excuse the fake code here, more meant to be conceptual)
position = element.location() #figure out location of our key_str
if element contains key_str: #check we are in right place
button_pos = position + 3 #if in right place, move to button element
action = ActionChains(driver)
# click the button
action.click(on_element = button_pos)
All advice is appreciated!

Clicking in selenium python after using move_by_offset()

I am trying to navigate through a miniature window containing a grid (1000+ rows) which lies inside of a website. And my goal requires me to click each row of the grid. Initially I tried locating each row using xPATH but there is no reliable way to do this because when I scroll down the grid, the xPATH changes (eg. row 1 -> ...div/div[1]/..., row n -> ...div/div[6]/...)
therefore to get around this issue, I used actionChains move_by_offset and located the row I want this way. and using a loop to add on some grid-width each time, before hitting some maximum number of rows that can appear on screen and scrolling down using a.send_keys(Keys.PAGE_DOWN).
This is all working well and i can see the hover animation above the rows when i want them. However, I cannot get the actionChains to click on the rows when hovering. I have tried click, double click and click_and_hold + release to no success.
Any help on either making move_by_offset + click work or using xPATH's more effectively would be great.
For context, I am using this on website for work so I cannot release the URL/photos of it. This is my first time using selenium.
for s in range(n):
scrollLim = 7
base = 180
for x in range(scrollLim):
print(str(x))
a.reset_actions()
a.move_by_offset(60, base + 60 * x).double_click()
a.perform()
time.sleep(1)
if x == scrollLim - 1:
a.send_keys(Keys.PAGE_DOWN).perform()

Locating element's screen position in Selenium (using Python)

Problem similar to the one described here.
I want to get the mouse from point a to point b along a curve that I define. So, I need a and b's location coordinates. Point a is the mouse's current location, and point b is an element that I can locate. I can find the element's position using the following:
element = driver.find_element_by_class_name("insertnamehere")
x, y = element.location["x"], element.location["y"]
Unfortunately, I can't use similar Selenium methods to find the mouse's current location (as far as I know). So I have to find the mouse like this:
import pyautogui
x_mouse, y_mouse = pyautogui.position()
Pyautogui gives screen location, which--as the above linked question described--is not what element.location gives. But the linked solution doesn't work for me on the site that I'm interested in. That is, I don't get matching coordinates. The site is this one.
I suspect it's because Selenium's locator doesn't count the map element as part of the web page, and returns coordinates relative to the listings (the part that you can scroll). So, here are some ideas I've tried:
Convert the screen location into Selenium location, or vice versa.
This is difficult because I think the formula (screen to Selenium) will depend on my monitor, so won't work generally.
Put the mouse on a web element so that point a can be located using Selenium's .location.
Might be the best solution, but I would have to move to the element using ActionChains(driver).move_to_element(), which defeats the purpose of defining an a -> b mouse path.
Either find the mouse location using Selenium's location method or find the element in terms of screen coordinates.
I've tried pyautogui's locateOnScreen(). Seems promising, and I will probably just do it this way.
If you are trying to get the absolute x and y values (x and y values relative to screen) then use the below logic.
# maximize the browser window so that you can get the absolute x and y based on the window inner and outer heights
driver.maximize_window()
# navigate to OP
driver.get("https://stackoverflow.com/questions/56688961/locating-elements-screen-position-in-selenium-using-python")
# get ask question element
askQuestion = driver.find_element_by_link_text("Ask Question")
# get the browser panel height
panel_height = driver.execute_script('return window.outerHeight - window.innerHeight;')
# get absolute x value
abs_x = askQuestion.location['x']
# get y value (this is relative y value - meaning y value with in browser)
y = askQuestion.location['y']
abs_y = y + panel_height
# print the absolute x and y values
print ("Absolute x : " + abs_x)
print ("Absolute y : " + abs_y)

Selecting a button, list object has no attribute click python selenium

After searching for a while an answer to my question, I couldn't get an answer that helped me, so here I am asking for your help ! :)
Right now, I am trying to select a plan on a website page which, after it has been selected (Read : a certain button clicked) displays the rest of the page where I can send the keys / values that I want to send.
Here is the code I am using
select_plan = browser.find_elements_by_xpath(".//*[#id='PostAdMainForm']/div[1]/div/div/div/div/div[1]/div[3]/button")
select_plan.click()
I found the xpath with Firepath, but when I run my code it gives me a AttributeError: 'list' object has no attribute 'click'
Here is the page I am trying to click from
https://www.kijiji.ca/p-post-ad.html?categoryId=214&hpGalleryAddOn=false&postAs=ownr
(I am looking to click on the left button, the one in blue)
Thank you very much for you help :)
The method find_elements returns a list, not a single element. You are taking the result and trying to click on it. Like the error says, you can't click on a list.
Either use find_element (singular) or use find_elements (plural) and then click on one of the elements that was returned.
# using find_elements
select_plans = browser.find_elements_by_xpath(".//*[#id='PostAdMainForm']/div[1]/div/div/div/div/div[1]/div[3]/button")
if len(select_plans) > 0:
select_plans[0].click()
# using find_element
select_plan = browser.find_element_by_xpath(".//*[#id='PostAdMainForm']/div[1]/div/div/div/div/div[1]/div[3]/button")
if select_plan:
select_plan.click()
Though, the link for the page you shared did not have the blue button. However, I have found it, after navigating to 'Post Your Ad' page. You can click on the Select button which is in blue color using the text appearing before it. For example, using text Basic, you can reach to the Select button. Following code shows, how we can achieve this:
select_plan = browser.find_element_by_xpath("//h3[text()='Basic']/following::button[text()='Select'][1]")
select_plan.click()
Let me know, whether it works for you.

python selenium - picking the right option with the right name

https://gyazo.com/b3e792b66775f64147671a6f23bd52c7
On the picture out from the first red dot, is there a list of options if u click on the "vælg" button (its in my language). I want my script to click on the right option.
Lets say the right one is "unisexur." How do I make my script click the option that says "unisexur" in the option list.
I know how to choose one of the options by e.g. xpath:
choice = browser.find_element_by_xpath('//*[#id="matrix-element-666"]/option[2]')
sleep(1)
choice.click()
which will make the code take the second option. So that is not what I want.
The html code is in the picture in the top..
You are probably looking for select_by_value.
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id('matrix-element-666'))
select.select_by_value('unisexur').click()

Categories

Resources