miles fryett
miles fryett

Reputation: 23

How to select, copy and paste everything within an element using selenium webdriver (python)

Here is basically what I'm trying to do. I have 2 websites, website A contains the data I need to move over to website B. In essence

I'm migrating data from website A to B as website A is going down soon.

What I need to move is not just text, it can be text, images or hyperlinked text, aswell as there is some format things that I need to keep. I think It is simplest to copy and past rather then store all of this data in a way that would allow me to insert it to website B the exact same way as if it was copy and pasted. Before me making a code solution they where just literally copying and pasting everything from A to B. Right now I have everything implemented(getting of links and everything else required) in my code but I cant move the data over. So basically here's what I'm doing right before I try to copy and paste the data. I am using python 3.

            original_window = driver.current_window_handle
            driver.execute_script("window.open()")
            wait.until(EC.number_of_windows_to_be(2))
            driver.switch_to.window(driver.window_handles[1])
            actURL = a.getlink()
            driver.get(actURL)
            e = a.getactivitydata(driver)
            driver.close()
            driver.switch_to.window(driver.window_handles[0])

Here A is a custom object and has the method get link which returns the link to website A that i need the data from. A also contains the method getactivitydata, which is where I want to select, copy and return the driver. The methods code is

def getactivitydata(self, driver):
    r = driver.page_source
    soup = BeautifulSoup(r, 'html.parser')  # Raw html obj
    ty = self.typef
    if ty == 'page':
        elem = driver.find_element_by_id("page-content")
        end = driver.find_element_by_class_name('course-nav')
        a = ActionChains(driver)
        #elem.send_keys("bar")
        a.move_to_element(elem)
        a.click_and_hold().perform()
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        a.move_to_element(end)
        a.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()

        #elem.send_keys(Keys.CONTROL, 'a')  # Select all
        #elem.send_keys(Keys.CONTROL, 'c')  # Copy
        return(elem)

    elif ty == 'quiz':
        pass
    elif ty == 'assign':
        pass
    elif ty == 'folder':
        pass
    elif ty == 'glossary':
        pass
    elif ty == 'resource':
        pass
    elif ty == 'forum':
        pass

ty represents the type of page as each page will need to be handled slightly differently. What I want this to do is basically select all of the text and images that is inside of the HTML with the element id 'page-content'. When ran the code(with plenty of other code that works) I get the following exception

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

This exception is being raised on the line where I try to actually copy the data I need.

While the element that actually contains the text is <h1, <h2... ect.(its closed >, this is my first time posting and I cant figure out how to get stack overflow to display it with it closed) Images are contained by <img, If I was to somehow run a loop through all of thease and highlight them all then copy and paste, how would I do that preserving the order the images show up within the text and how would I get the amount of <hn as it would be different for every page. I have tried a few different elements/ methods to try to select and copy the text and so far I have not been able to successfully highlight any text(That I can visually see, I use chrome webdriver 89).

The smallest HTML tag they all belong to is <div class ="row" and there are multiple rows on even a basic single box of text/images.

Any help or guidance is welcomed, I'm not against using another method other then copy/paste but I do need it to output on website B as if it had been copy/pasted. as well there are types of pages that have multiple separate elements, (Like an online quiz, where you have question 1, a)... , b...) ... question 2) ect...) Thank you!

Upvotes: 2

Views: 2472

Answers (1)

miles fryett
miles fryett

Reputation: 23

So just to update anyone down the line trying to do the same thing. clicking a dragging did not work, what did was

    a = ActionChains(driver)
    #elem.send_keys("bar")
    elem = driver.find_element(By.ID, "maincontent")
    #wait = WebDriverWait(driver, 10)
    #first = wait.until(EC.element_to_be_clickable(elem))
    a.move_to_element_with_offset(elem, 0, 0)
    a.key_down(Keys.SHIFT)
    a.double_click(elem).double_click(elem)
    end = driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[3]')
    a.move_to_element(end).double_click(end)
    a.key_up(Keys.SHIFT)
    a.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()

basically here what Im doing is holding shift and quad clicking(I think 3 would do technically) at the start of the element and at another element that's always below it, I tried to implement and offset but could not get it to work

Upvotes: 0

Related Questions