Python Selenium chromedriver - Action Chain (Control + щелчок) открывает URL-адрес в новом окне, а также в текущем окне для тега без привязки - PullRequest
0 голосов
/ 25 февраля 2019

Существует тег div, которым я хочу управлять, щелкая с помощью хромового драйвера селена.Как только выполняется цепочка действий, открывается новая вкладка с нужной ссылкой (новый URL), но это также вызывает щелчок по текущему / главному окну, и новый адрес открывается в главном окне.Ниже приведен код:

class Scraper:
    def __init__(self):
        self.driver = webdriver.Chrome('/usr/bin/chromedriver')
        self.wait = WebDriverWait(self.driver, 10)

    def get_last_line_number(self):
        return len(self.driver.find_elements_by_xpath('//div[contains(@class, "gwt-Label") and contains(@class, "WJSO") and contains(@class, "WPTO")]'))

    def get_links(self, max_company_count=15000):

self.driver.get ('https://ascenaretail.wd5.myworkdayjobs.com/us_corporate_jobs') self.wait.until (EC.visibility_of_element_located ((By.CSS_SELECTOR, "div.WN1N")))

    main_window = self.driver.current_window_handle
    last_line_number = 0
    while last_line_number < max_company_count:
        self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        self.wait.until(lambda driver: self.get_last_line_number() != last_line_number)
        last_line_number = self.get_last_line_number()
        all_elements = self.driver.find_elements_by_xpath('//div[contains(@class, "WJYF") and contains(@class, "WHYF")]')
        data_array = []
        for element in all_elements:
            title_element = element.find_element_by_xpath('.//div[contains(@class, "gwt-Label") and contains(@class, "WPTO") and contains(@class, "WJSO")]')
            element_text = title_element.text
            location_element = element.find_element_by_xpath('.//span[contains(@class, "gwt-InlineLabel") and contains(@class, "WM-F") and contains(@class, "WLYF")]')
            ActionChains(self.driver).key_down(Keys.CONTROL).click(title_element).key_up(Keys.CONTROL).perform()
            self.driver.switch_to_window(self.driver.window_handles[1])

            self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.GWTCKEditor-Disabled")))

            print(self.driver.current_url)
            self.driver.switch_to_window(main_window)
            print(self.driver.current_url)
            break
        break    
    return True;

if __name__ == '__main__':
    scraper = Scraper()
    company_links = scraper.get_links(max_company_count=146)

ActionChains работает правильно и открывает URL-адрес в новой вкладке, но также открывает URL-адрес в текущей вкладке, и я теряю контроль над вкладкой

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...