I tried to use ActionChains to duplicate tab with shortcut key ALT+D and ALT+ENTER. And here's the code from user:5168011:
action_chains = ActionChains(driver)
action_chains.key_down(Keys.ALT).send_keys('d').perform()
action_chains.key_down(Keys.ENTER).perform()
action_chains.key_up(Keys.ALT).key_up(Keys.ENTER).perform()
However, it just doesn't work. I've tried the code both using Chrome and Firefox as web-driver, tried in both Linux and Win10, and also tried Keys.ALT and Keys.LEFT_ALT and even used send_keys function to send the Unicode of ALT and LEFT_ALT. But it just looked like it didn't press the ALT key at all.
I suspect that ALT key didn't be pressed because of my another test: if I select an input box as element
and use the code:
action_chains.key_down(Keys.ALT, element=element).key_down(Keys.SHIFT, element=element).send_keys('d').perform()
it will show "D" in the text box. However, in reality if I chick the text box and press ALT+SHIFT+d, it will show nothing. So I suggest that key_down(Keys.ALT, element=element)
doesn't work and it looks just like action_chains.key_down(Keys.SHIFT, element=element).send_keys('d').perform()
and that's why it will show "D" in the text box.
By the way, after test I found that my Keys.SHIFT and Keys.CONTROL works well, so maybe the problem is just for ALT. Is this because of the selenium version? I searched the Internet and didn't find the similar problem...