Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用selenium python从网站中单击按钮后查找URL_Python_Selenium_Url_Selenium Firefoxdriver - Fatal编程技术网

使用selenium python从网站中单击按钮后查找URL

使用selenium python从网站中单击按钮后查找URL,python,selenium,url,selenium-firefoxdriver,Python,Selenium,Url,Selenium Firefoxdriver,网站的每个按钮都可能包含链接,下面的网站如何找到URL将出现在下一个选项卡中 要在单击按钮后打印和刮取URL 我正在使用firefox web驱动程序 driver.get("https://www.dove.com/us/en/skin-care/body-lotion/cream-oil-intensive-body-lotion.html") driver.find_element_by_xpath("//span[contains(text(),'Ingre

网站的每个按钮都可能包含链接,下面的网站如何找到URL将出现在下一个选项卡中

要在单击按钮后打印和刮取URL 我正在使用firefox web驱动程序

driver.get("https://www.dove.com/us/en/skin-care/body-lotion/cream-oil-intensive-body-lotion.html")
driver.find_element_by_xpath("//span[contains(text(),'Ingredients')]").click()
time.sleep(3)
driver.find_element_by_xpath("//button[contains(text(),'Go to SmartLabel™')]").click()

这应该很简单,只需使用
驱动程序。当前url
。所以用你的代码你可以试试

driver.get("https://www.dove.com/us/en/skin-care/body-lotion/cream-oil-intensive-body-lotion.html")
driver.find_element_by_xpath("//span[contains(text(),'Ingredients')]").click()
time.sleep(3)
driver.find_element_by_xpath("//button[contains(text(),'Go to SmartLabel™')]").click()
time.sleep(5)
driver.switch_to.window(driver.window_handles[1])

print(driver.current_url)
我看到了一些问题:

1等待。摆脱时间。睡眠()。将其替换为显式/隐式等待。我注意到这些元素是页面上最后加载的:
picture[class='loaded']
。所以,我加了一句等待他们

2要在选项卡之间切换,请使用:
driver.switch\u To.window(driver.window\u句柄[1])
driver.switch\u To.window(driver.window\u句柄[0])
-切换到初始选项卡

铬解决方案

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
# driver.implicitly_wait(10)
driver.get("https://www.dove.com/us/en/skin-care/body-lotion/cream-oil-intensive-body-lotion.html")
wait = WebDriverWait(driver, 30)
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "picture[class='loaded']")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".collapsed>a[title='Ingredients']")))
driver.find_element_by_css_selector(".collapsed>a[title='Ingredients']").click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Go to SmartLabel')]")))
driver.find_element_by_xpath("//button[contains(text(),'Go to SmartLabel')]").click()
driver.switch_to.window(driver.window_handles[1])
print(driver.current_url)
driver.close()
driver.switch_to.window(driver.window_handles[0])
print(driver.current_url)
输出:

https://smartlabel.unileverusa.com/011111375512-0001-en-US/index.html
https://www.dove.com/us/en/skin-care/body-lotion/cream-oil-intensive-body-lotion.html
对于Firefox,您需要在第二个页面上等待至少一个元素,否则输出将不会为您提供预期的链接:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.get("https://www.dove.com/us/en/skin-care/body-lotion/cream-oil-intensive-body-lotion.html")
wait = WebDriverWait(driver, 30)
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "picture[class='loaded']")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".collapsed>a[title='Ingredients']")))
driver.find_element_by_css_selector(".collapsed>a[title='Ingredients']").click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Go to SmartLabel')]")))
driver.find_element_by_xpath("//button[contains(text(),'Go to SmartLabel')]").click()
driver.switch_to.window(driver.window_handles[1])
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".container-fluid.content-section")))
print(driver.current_url)
driver.close()
driver.switch_to.window(driver.window_handles[0])
print(driver.current_url)

另外,如果您正在寻找一种通过属性名称查找链接的方法,则没有任何方法,因为此按钮没有此类链接。链接已生成。

哦,我通过查看网站发现了问题所在。“转到SmartLabel”按钮打开一个新选项卡,您需要告诉Selenium切换到该选项卡。给我一分钟,我会更新我的答案,告诉你怎么做。是的,这是找到链接的其他方法吗?@Sandhya你看到我的最新更新了吗?一切正常!检查我的答案。OP使用的是Firefox,而不是Chrome;它可能会起作用,但仍然有效。2.虽然等待是一个很好的建议,但它们实际上并没有解决OP最初的问题。此外,由于
wait
返回一个webElement,因此您可以编写
wait.until(EC.element可点击((By.XPATH,“//按钮[包含(text(),'Go to SmartLabel')))))))))。单击()
以避免一些额外的行(和缓慢的XPATH)1。添加了Firefox2的解决方案。我怀疑时间会明显变慢,我甚至怀疑它会变慢。此外,它完全解决了他们的问题,因为网站可能无法在3秒钟内加载,而且
成分
选项卡可能无法访问
time.sleep()
是我们最不应该使用的东西。我同意,硬等待是最后一种手段,或者只是在您完成正确的动态等待之前将其放置到位。在选项卡之间切换window_句柄[1]window_句柄[0])是正确的方法,但显式/隐式等待会引发错误。您在哪一行看到错误?您已经可以对答案进行投票,因此如果答案有帮助,请这样做。