Right now, my code screenshots a specific section of a webpage. However, since the element it's screenshotting is quite small on the webpage, the picture is also quite small. So, to solve this issue, I thought I could zoom into the page, center onto the element, screenshot the element, and then zoom out – but it's not working.
When I run the following code (no zooming in):
theComment = driver.find_element(By.XPATH, "/html/body/div[1]/div/div[2]/div[3]/div/div/div/div[2]/div[1]/div[2]/div[6]/div/div/div/div[1]")
driver.execute_script('arguments[0].scrollIntoView({block: "center"});', theComment)
theComment.screenshot('comment'+str(verified+1)+'.png')
I get the following image:
But when I run the following code (zooming in):
theComment = driver.find_element(By.XPATH, "/html/body/div[1]/div/div[2]/div[3]/div/div/div/div[2]/div[1]/div[2]/div[6]/div/div/div/div[1]")
driver.execute_script("document.body.style.zoom='150%'")
sleep(1)
driver.execute_script('arguments[0].scrollIntoView({block: "center"});', theComment)
sleep(1)
theComment.screenshot('comment.png')
sleep(1)
driver.execute_script("document.body.style.zoom='100%'")
I get the following image (the screenshot is really far away from where the actual comment is, and smaller than the comment is):
Any help would be greatly appreciated. Thank you.
Related
I am writing a program that answers questions from a database. I am currently on the last question type (ordering questions). Here is an image of what the question looks like. The user needs to put the elements in the box in order.
As you can see, the bottom of the question gets cut off. This caused errors when trying to click certain elements that were below the page. To try and solve this I scrolled to the bottom of the page but selenium scrolled back to the top before clicking which kept the error in place. And after trying a few other things I decided to try and zoom out on the webpage to see them all at once.
This is the HTML of the list where the <p> tag is what I am trying to click on. Actually, I am trying to right-click but same thing. The rest of the <div>s with the ellipses are the rest of the boxes and are the same as the first one.
newheader = self.driver.find_element(By.XPATH, "//div[contains(#class, 'responses-container')]")
action = ActionChains(self.driver)
action.context_click(WebDriverWait (newheader, 10).until(EC.presence_of_element_located((By.XPATH, "//p[contains(text(), '"+ dictionary_.get(prompt)[r] +"')]")))).perform()
# dictionary.get(prompt)[r] contains the values we are looking for (ovulation, fertilization, etc)
# I have also tried EC.element_to_be_clickable and had the same results
I have tested this on a question that does not overflow the page and it works perfectly. The only time I am getting errors is when I try to zoom out. I have tried refreshing the page before starting the clicking and after zooming out. I have also tried resetting the driver by calling
self.driver.get(self.driver.current_url)
Each of those elements cannot be left-clicked on to select. They can only be right-clicked or tabbed to. Tabbing requires calculations that are complex since each element is being moved dynamically so I chose to right-click instead. It does right-click but in seemingly random places now that it's zoomed out.
Any help is appreciated! Thanks!
so I want to do an automated testing on an iframe (input image). Here is what I have done so far:
WebDriverWait(driver, 1000).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element("xpath","//div[#id='el_banner_url_upload']//iframe")))
upload_image = driver.find_element("xpath", "//input[#name='images']")
upload_image.send_keys(str(d_image))
driver.switch_to.default_content()
For the flow:
I click on a button inside the iframe
It opens my windows folder, then I select a picture I want to upload
The image uploaded and it gives a preview + a link to the image that is generated automatically by the website is shown on the field below the displayed image
Here is the link to the screenshot of what I'm talking about:
Before uploading the image
After uploading the image
I got 2 problems right now,
After running the script, it works well (didn't give any error on the command). The "upload" button is gone now. But, the image is not shown on the display and there are no link generated. As shown in this screenshot. Tried inspect it, and found this error on the console:
Uncaught TypeError: Cannot read properties of undefined (reading 'showQuality')
at HTMLFormElement.add (pic-embed.js:173:35)
at $.<computed>.<computed>._trigger (jquery.ui.widget.js:489:13)
at File.<anonymous> (jquery.fileupload.js:846:31)
at Function.each (jquery.js:4:5347)
at $.<computed>.<computed>._onAdd (jquery.fileupload.js:840:15)
at $.<computed>.<computed>._onAdd (jquery.ui.widget.js:105:25)
at Object.<anonymous> (jquery.fileupload.js:1016:26)
at c (jquery.js:4:26036)
at Object.add [as done] (jquery.js:4:26346)
at Object.always (jquery.js:4:27212)
The iframe would randomly not refreshed correctly, this too I couldn't figure out how. If this happens, the script couldn't run. Here is the screenshot of when the iframe didn't refresh correctly
Are there any solution to this?
You should use Selenium expected conditions. Don't just find elements and click them, wait for them to be enabled or clickable:
driver.wait.until(ExpectedCondition.element_to_be_clickable((By.XPATH, "myXpath"))).click()
driver.wait.until(ExpectedCondition.element_to_be_clickable((By.CSS, "myCSS"))).click()
When you try to click on an element before it is actually clickable - you are going to get errors.
I have been trying to scrape a leaflet concert map(see picture below), where the information only shows if you hover over the seat with your mouse. I have searched the internet up and down, I slowly start to understand where the information comes from by going into the Network Tab, but I still cannot fully grasp it.
This is the link to the page
Where is this information saved and how can I access it? I would everything that shows up when you hover over the map and see in the small box or on the side when you select it (everything that is found in class="tickettype-item")
I tried something like this, but I'm sure it's nowhere near enough. I would greatly appreciate the help as I'm really keen on understanding what's happening here and how I can solve this.
list_of = driver.find_element_by_xpath('//*[#id="seatmap-tab"]/div[2]/div/div/section/div[2]/div[2]/div[2]/div/div[2]/div[2]/div[2]/div[2]/canvas')
for item in list_of:
action = ActionChains(driver)
action.move_to_element(item)
try:
description = wait(driver, 3).until(EC.visibility_of_element_located((By.XPATH, '//*[#id="seatmap-tab"]/div[2]/div/div/section/div[2]/div[3]/div[2]/div[2]/div/div'))).text
print(description)
action.perform()
except:
action.perform()
In python I have:
driver = webdriver.Safari()
driver.get("https://ug3.technion.ac.il/rishum/login")
driver.find_element_by_id("username").send_keys(user_id)
driver.find_element_by_id("password").send_keys(password)
driver.find_element_by_class_name("btn btn-success").click()
But for some reason it doesn't click on sign bottom (the green one) how can I fix this?
Update: from the comments I got I understand that I need to solve the captcha thus I want now to click on the Audio bottom, in a tutorial they wrote:
driver.switch_to.default_content()
frames = driver.find_element_by_xpath("/html/body/div[2]/div[4]").find_elements_by_tag_name("iframe")
driver.switch_to.frame(frames[0])
delay()
# click on audio challenge
driver.find_element_by_id("recaptcha-audio-button").click()
but that didn't work for me.
I am trying to use Selenium with Python to click on a text field, which opens a pop-up panel, select the text entry area of that popup, and enter text to it.
switch_to_window and switch_to_frame don't seem to be working. In a previous question I asked about Selenium, someone told me to pause the program until the element I need is available. The solution worked for that problem, but not this one, so I'm assuming I have a different issue and I'm too new to Selenium to understand what it is.
This is what the original box I'm trying to click on looks like:
And the Inspect Element for this box:
When that description box is clicked, it should open this window:
And select this element to enter text into:
So in my code I have:
descriptionBox = driver.find_element_by_id('kiadvany_fulszoveg_text')
descriptionBox.click()
That does not error the program, but it also doesn't seem to actually be clicking on that element. To make matters more confusing, I got this to work exactly ONCE, where it opened the correct Description text box as pictured above, but it has since not worked at all even when I try the exact same thing.
The panel's ID is:
As I mentioned, switching to this panel ID using switch_to_frame or switch_to_window was the first thing I tried, but I'm getting a No Such Element error.
Because I saw the description box open correctly once, but never again, I'm assuming that's where the problem is. I wish, the one time it did pop up, that I'd tried to put the text into the field to see if that would work too, but I hadn't gotten there yet at that point, so I don't know if that would have worked.
Thank you in advance to anyone who can help with this!
Try this
descriptionBox = driver.find_element_by_id('kiadvany_fulszoveg_text')
driver.execute_script('arguments[0].click();', descriptionBox)
or
actions = ActionChains(driver)
actions.move_to_element(descriptionBox)
actions.click(descriptionBox)
actions.perform()